I am encountering a weird issue here...
After I seem to successfully insert some data into my db.collection
I cant seem to get it to reflect using db.collection.find().fetch()
.
Find below the code I insert into my chrome console:
merchantReviews.insert({merchantScore: "5.5"}, function() {
console.log("Review value successfully inserted");
});
This yields:
"9sd5787kj7dsd98ycnd"
Review value successfully inserted
I think returned value "9sd5787kj7dsd98ycnd"
is an indication of a successful db collection insert. Then when I run:
merchantReviews.find().fetch()
I get:
[]
Can anyone tell me what is going on here?
Looking forward to your help.
There are two possibilities here: either the insert fails on the server even though it passes on the client, or you haven't subscribed to your collection.
In case the insert
fails on server (most likely due to insufficient permissions, if you have removed the insecure
package but have not declared any collection.allow
rules), the client code still returns the intended insert ID (in your case, "9sd5787kj7dsd98ycnd"
). The callback is called once the server has confirmed that the insert has either failed or succeeded. If it has failed, the callback is called with a single error argument. To catch this, you can instead insert the document like this:
merchantReviews.insert({merchantScore: "5.5"}, function(error) {
if (error) {
console.error(error);
} else {
console.log("Review value successfully inserted");
}
});
If this still logs successful insert, then you haven't subscribed to the collection, and you have removed the autopublish
package. You can read about Meteor publish-subscribe system here. Basically, you have to publish
the collection in server-side code:
Meteor.publish('reviews', function () {
return merchantReviews.find();
});
And in server code (or your js console) you need to subscribe to the collection with Meteor.subscribe('reviews')
. Now calling merchantReviews.find().fetch()
should return all documents in the collection.