Loop over the Mongodb objects to get the values in Meteorjs - mongodb

I am first Time working With Meteorjs and MongoDB. I am working on a Q&A application. where I have a list of objects which i got using Meteor database call. I want to looper over the list and want to split one of its attribute to the length 50 and then want to send a list of these splitted objects to the database.what I have is this
Template.questions.questions = function () {
questions= Meteor.questions.find({topic_id: Session.get("currentTopicId")});
return questions
}
Every question object in the questions list has an attribute question_text. Using loop i want to split this attribute to length of fifty and then want to push that to an empty list and return that list . Like
questions_list=[]
for(i=0;i<questions.length;i++){
question=questions[i].question_text[0:50] // python syntex to get first 50 char of a string
questions_list.push(question
}
return questions_list
and My HTML is like
<tbody>
{{#each questions}}
<tr>
<td class="span2" style="overflow:hidden;width:50px">
{{question_text}}
</td>
</tr>
{{/each}}
</tbody>
suggest me guys how can i acieve this in meteorjs. my problem is when i try to loop over this questions list there are many attributes like Collection, Results, Queries. here i am unable to iterate this list of objects.
In the same way how can i get the error message thrown back by meteorjs

This will give you list of shortened texts of all questions matching a query:
var questions = Questions.find({...}).map(function(question) {
return question.text.substr(0,50);
});
You could use it directly in a helper:
Template.questions.questions = function () {
return Questions.find({...}).map(function(question) {
return question.text.substr(0,50);
});
};
By the way, Questions.find({...}) does not return a list of questions, but a cursor object that you can use to manipulate query data in an effective way (like that map method above). To get raw array, you need to use fetch on that cursor: Questions.find({...}).fetch().

If you're just displaying the data you could create a handlebars helper:
Template.questions.helpers({
first50: function(question) {
return question.text.substr(0,50);
}
});
Then add the helper:
Template.questions.questions = function() {
return Questions.find({...});
};
Then in your template you could do:
<tbody>
{{#each questions}}
<tr>
<td class="span2" style="overflow:hidden;width:50px">
{{first50 question_text}}
</td>
</tr>
{{/each}}
</tbody>

Related

How can I extract an attribute from multiple elements in Playwright?

I'm trying to extract some attributes from multiple elements using playwright.
( this is F# but you should be able to figure out what is wrong )
let! pagelinkRows = page.EvalOnSelectorAllAsync("tr[title]","(tr) => tr.getAttribute("title")") |> Async.AwaitTask
I get the error: tr.getAttribute is not a function. I have also tried tr.attr .
What is the simplest way to get the attributes from a collection of elements
<tr title="xxx1"></tr>
<tr title="xxx2"></tr>
<tr title="xxx3"></tr>
<tr title="xxx4"></tr>

Finding the contents under div with specific id patterns using MOJO::DOM

I need to parse some HTML codes. The patterns of the tag ID are:
<tr id="date">.....</tr>
<tr id="band01"><td>field1</td><td>field2</td></tr>
<tr id="band02">...contents...</tr>
.....
<tr id="(others">.....
I'm using PERL Mojo::DOM parser, and want to extract all the actual ids with names starting with "band" following by a number, as well as its contents.
How could I achieve this?
The E[foo^="bar"] selector matches any element with a "foo" attribute starting with "bar". Thus you can use:
my $dom = Mojo::DOM->new($html);
my $rows = $dom->find('tr[id^="band"]');
$rows would be a Mojo::Collection of Mojo::DOM objects representing each matching element and its respective contents. For example, to get the list of matched IDs:
my #ids = $rows->map(attr => 'id')->each;
Or with more standard Perl:
my #ids = map { $_->{id} } #$rows;

How to retrieve only part of a document back in a call

I need to retrieve only part of a document and call it via a helper so that I can render a subtemplate multiple times as the part I require to pull from the db is an array of object itself. I have the following as the fields. What I need to do with my helper is only retrieve the ordersDispatch array of one particular document which would be uniquely called by the tripNumber field.
I have tried several things but nothing has come close to only having an array of the objects in the orderDisptach field be returned in a fashion that it can be used by the helper to render my subtemplate for each object in the array.
{
tripNumber: companyRecord.lastTripNum + 1,
custID: $('input:hidden[name=orderCustomerId]').val(),
custContact: $('input:text[name=customerContact]').val(),
custEmail: $('input:text[name=customerEmail]').val(),
trailerSealNum: $('input:text[name=trailerSealNum]').val(),
orderBroker: $('input:text[name=orderBroker]').val(),
orderEquipment: $('input:text[name=orderEquipment]').val(),
orderLoadNum: $('input:text[name=orderLoadNum]').val(),
orderPlacedDate: $('input:text[name=orderPlacedDate]').val(),
orderPrivateNotes: $('textarea[name=orderPrivateNotes]').val(),
orderPublicNotes: $('textarea[name=orderPublicNotes]').val(),
orderCurrency: $("input[name=orderCurrency]:checked").val(),
orderCharges: $('input:text[name=orderCharges]').val(),
orderFUELCheck: $('input:checkbox[name=orderFUELCheck]').is(':checked'),
orderFUELPerc: $('input:text[name=orderFUELPerc]').val(),
orderFUELTotal: $('input:text[name=orderFUELTotal]').val(),
orderGSTCheck: $('input:checkbox[name=orderGSTCheck]').is(':checked'),
orderGSTPerc: $('input:text[name=orderGSTPerc]').val(),
orderGSTTotal: $('input:text[name=orderGSTTotal]').val(),
orderPSTCheck: $('input:checkbox[name=orderPSTCheck]').is(':checked'),
orderPSTPerc: $('input:text[name=orderPSTPerc]').val(),
orderPSTTotal: $('input:text[name=orderPSTTotal]').val(),
orderTAXCheck: $('input:checkbox[name=orderTAXCheck]').is(':checked'),
orderTAXPerc: $('input:text[name=orderTAXPerc]').val(),
orderTAXTotal: $('input:text[name=orderTAXTotal]').val(),
orderTotalCharges: $('input:text[name=orderTotalCharges]').val(),
ordeBlockInvoicing: $('input:checkbox[name=ordeBlockInvoicing]').is(':checked'),
orderExtraCharges: orderExtraCharges,
orderPickups: puLocations,
orderDeliveries: delLocations,
orderDispatch: dispatchLocations,
createdDate: new Date(),
createdUser: currentUser.username
Any help in building a helper that will accomplish this would be greatly appreciated as I am new to meteor and mongo.
The following helper should give you what you need:
Template.oneTrip.helpers({
orderDispatch: function(tn){
return Trips.findOne({ tripNumber: tn }).orderDispatch;
}
});
Trips.findOne({ tripNumber: tn }) gets you an individual document and .orderDispatch returns the value of the orderDispatch key which in your case will be an array.
html:
<template name="oneTrip">
{{#each orderDispatch this._id}} <!-- assuming you've already set the data context to an individual order -->
{{this}} <!-- now the data context is an element of the orderDispatch array -->
{{/each}}
</template>

Sorting a list of items into different categories

Imagine a site that lists different articles with different subjects, and you as a user can mark your favorite articles to read later. Favorited articles get saved and displayed in your personal little space on this site like this:
Movies
Upcoming summer blockbusters
Will there never be a end of Superheroes?
Are romcoms dead?
Science
Bezos or Musk, who will reach Mars first?
Philosophy
How can mirrors be real if our eyes aren't real?
Etc, etc.
I have saved each article in the database with the following fields:
{
articleName: "Upcoming summer blockbusters",
subject: "Movies",
link: //link to the article
}
Then when a user favorites one I simply duplicate it into his own collection in the database.
Then comes the problems...
I could iterate through his articles and print them out on his user page like so:
<ul>
{{#each articles}}
<heading>{{subject}}</heading> //how do I avoid duplicates?
<li>{{articleName}}</li>
{{/each}}
</ul>
This, however, would duplicate the subjects that are shared across multiple articles.
I could iterate through the subjects only (making them into an array that checks for duplicates, for instance) and print them out:
<ul>
{{#each subjects}}
<heading>{{this}}</heading>
<li>{{../articleName}}</li> //how do I print the correct one?
{{/each}}
</ul>
But this way would be completely broken, since the articles wouldn't show up under the correct subject headings...
How should I proceed?
Actually I think you should store favorite articles only, the way you did. It will be way simpler to handle cases like article deletions. Just parse the articles in a helper function, and you should get the same result:
favArticlesBySubjects: function () {
var favArticles = Meteor.user().profile.favArticles;
var articlesBySubject = [];
favArticles.forEach(function (article) {
var index = _.findIndex(articlesBySubject, function (obj) {
return obj.subject === article.subject;
});
if (index < 0)
articlesBySubject.push({subject: article.subject, articles:[article]});
else
articlesBySubject[index].articles.push(article);
};
return articlesBySubject;
}
This way, you can display your favorite articles using :
{{#each favArticlesBySubjects}}
<heading>{{subject}}</heading>
<ul>
{{#each articles}}
<li>{{articleName}}</li>
{{/each}}
</ul>
{{/each}}

Entity framework in MVC 4 - Include returning false result

I have two tables:
Products:
ProductID, ProductName
Compounds:
CompoundID, ComponentID, Qty
bound as next:
1 Products.ProductID --> many Compounds.CompoundID
1 Products.ProductID --> many Compounds.ComponentID
A Products item can be a compound, a components, or both.
I need that a view returns for each compound its components names and Quatities.
MyController method :
public ActionResult CompoundProduct()
{
var Compounds = db.Compounds.Include(s => s.Products);
return View(Compounds.ToList());
}
MyView code :
#model IEnumerable
<table>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.CompoundID)
</td>
<td>
#Html.DisplayFor(modelItem => item.ComponentID)
</td>
<td>
#Html.DisplayFor(modelItem => item.Products.ProductName)
</td>
</tr>
}
</table>
With this method I get a list of repeated Compound name instead of distinct Component names per Compound as shown in the image below:
any help please ?
When you created the model from the database EF should have created two navigation properties Products and Products1 related to the two foreign keys CompoundID and ComponentID. You are probably using the wrong navigation property that belongs to CompoundID, not the one that belongs to ComponentID. Instead of Products try to use Products1 in the Include...
var Compounds = db.Compounds.Include(s => s.Products1);
...and in the view:
#Html.DisplayFor(modelItem => item.Products1.ProductName)