Jerome
Jerome

Reputation: 1192

How to do a request "select article where title = "x" " with MongoDB

I'm trying to get the ownerID of article

where article.title = actualTitle (<-- I use a loop the iterate over each article on the page)

But it sounds impossible or I'm just not in the right way. This is my JavaScript

'click #showMesArticles'(event){
  var id = Meteor.userId();
  title = "";
  $('.titreArticle').each(function(i, obj) {
    title= obj.textContent;
    var art =  Articles.find({titre: title });
    alert("art.titre: "+ art.titre);
  });
 },
});

And if I look art is an [object Object] and art.titre is undefined

Someone could help me please ?

Upvotes: 0

Views: 56

Answers (2)

lnmunhoz
lnmunhoz

Reputation: 115

The [object Object] that returns from Articles.find is actually a MongoDB Cursor, which doesn't contain the data on itself.

So to get the data from this cursor you need to do a fetch in to the cursor.

const articles = Articles.find({title: 'x'}).fetch()
console.log(articles)

You can explore more on the Meteor Docs.

Upvotes: 1

Jerome
Jerome

Reputation: 1192

If someone is trying to do that the solution is the one of @Mohsen89z and you need to use findOne

'click #showMesArticles'(event){
  var id = Meteor.userId();
  title = "";
  $('.titreArticle').each(function(i, obj) {
    title= obj.textContent;
    var art =  Articles.findOne({titre: title });
    alert("art.titre: "+ art.titre);
  });
 },
});

Upvotes: 0

Related Questions