Fred J.
Fred J.

Reputation: 6039

Meteor mongodb insert document of an array

This code has the server insert some documents in a collection for the client to find it later.
I need to return the array for a given task But the page is saying

No data received

Why is that and how to fix it? Thanks

//Both
FooterButtons2 = new Mongo.Collection('footerButtons2');

//Server
Meteor.publish('footerButtons2', function(){
  return FooterButtons2.find();
});

FooterButtons2.insert(
  { "task1": ["submit"]},
  { "task2": ["cancel","continue"]}
);

//client
Meteor.subscribe('footerButtons2');
var res = FooterButtons2.findOne("task1");

Upvotes: 0

Views: 74

Answers (1)

Alex Tondello
Alex Tondello

Reputation: 141

When you search like this:

var res = FooterButtons2.findOne("task1");

you are searching an object that has the "_id" key equal to "task1", this is not correct. You want the object that has the key "task1" in it. The correct way would be:

var res = FooterButtons2.findOne({
    task1: { $exists: true }
});

But ideally, you should be doing searches based on values and not keys. Something like this:

FooterButtons2.insert({
    task: "task1",
    buttons: ["submit"]
}, {
    task: "task2",
    buttons: ["cancel", "continue"]
});

var res = FooterButtons2.findOne({
    task: "task1"
});

Upvotes: 1

Related Questions