Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor insert into collection appears to work, but remains empty

I'm doing a simple insert into a meteor collection that appears work, but leaves the collection empty.

The collection is defined properly on the server:

Meteor.publish("comments", function () {
return Comments.find();
});

Subscribed to properly in the client.js:

Meteor.subscribe("commments");

And set up properly on the model.js:

Comments = new Meteor.Collection("comments");

The insert code is as follows:

Meteor.methods({ 
    addComment: function (options) {
    check(options.post_id, String);
    check(options.comment, NonEmptyString);

    if (! this.userId)
        throw new Meteor.Error(403, "You must be logged in to comment.");
    if (options.comment.length > 1000) 
        throw new Meteor.Error(413, "Comment is too long");
    var post = Posts.findOne(options.post_id);
    if (! post)
        throw new Meteor.Error(404, "No such post");
 // add new comment
    var timestamp = (new Date()).getTime();
    console.log('Comment: ' + options.comment);
    console.log('Post: ' + options.post_id);
    console.log('UserId: ' + this.userId);
    var saved = Comments.insert({
        owner: this.userId,
        post_id: options.post_id,
        timestamp: timestamp,   
        text: options.comment});
    console.log('Saved: ' + saved);
   }
});

Once the insert is called, the console prints out the following:

Comment:  Something 
Post: xRjqaBBEMa6qjGnDm 
UserId: SCz9e6zrpcQrKXYWX 
Saved: FCxww9GsrDsjFQAGF 
> Comments.find().count()
0

I have inserts into several other collections that work just fine (Posts being one of them as you can see the post ID in the code). In the docs ist said that if the insert errors out it will print to the console, but as you can see it appears to be working, yet is actually empty.

Thanks.

UPDATE: I did find that the data is being put into the database, but for some reason is not showing up. I'm not sure why the data is not being published properly since there are no filters on the find().

like image 411
Erick Avatar asked Jul 01 '13 04:07

Erick


Video Answer


1 Answers

I'm not sure exactly what's wrong, but there's a few things to check here.

• First, this:

Meteor.publish("comments", function () {
    return Comments.find();
});

directs the server to publish the Collection, but doesn't actually establish the collection server side.

You should have Comments = new Meteor.Collection("comments"); available on both the client and the server. I tend to put in a file called model.js like the examples tend to do.

• Second possibility, you don't have a subscribe function shown above, such as Meteor.subscribe("comments"); If you don't have a subscribe function, your client isn't going to know about it, even though it does exist in the collection.

You can test this theory by typing meteor mongo in the shell (with your Meteor app running), and db.comments.find() to see if your comments are actually in the database but not subscribed to.

like image 104
emgee Avatar answered Sep 29 '22 06:09

emgee