0

I am new to javascript and very new to Meteor. Is this code correct? I need to define a function which will take an array of values and insert them in a Meteor Collection "FooterButtons"?

Client code

replaceCollectionContents(['NO', 'B', 'YES']);

Both code

replaceCollectionContents = function (buttonsList) {
  FooterButtons.remove();
  for(i = 0; i < buttonsList.length; i++) {
    FooterButtons.insert(buttonsList[i]);
  }
};
2
  • Care to leave a comment as to what improvement you could have done to write this question so that other may learn how to avoid writing a question which attached down-voting? :) Commented Feb 12, 2016 at 18:36
  • stackoverflow.com/help/mcve. What specifically have you tried, what specific error are you seeing, etc. Commented Feb 12, 2016 at 18:41

2 Answers 2

1

You cannot directly insert a string to a collection. The insert method expects a document which is of type object.

Try this instead -

FooterButtons.insert({ text: buttonsList[i] });

Also, I notice that you are trying to clear your FooterButtons collection. Please note that you cannot clear a collection like this from the client side as it is considered untrusted code. From client side, you can only remove one document at a time, specified by its _id.

I would recommend you to use a method instead.

Meteor.methods({
  replaceCollectionContents: function (buttonsList) {
    // remove all existing documents in the collection
    FooterButtons.remove({});

    // insert new button documents into the collection
    buttonsList.forEach(function (button) {
      FooterButtons.insert({ text: button });
    });
  }
});

And call this method when needed

Meteor.call("replaceCollectionContents", ['NO', 'B', 'YES']);

Inside the method, note that I'm passing a {} selector to the remove method because for safety reasons, Meteor does not remove any documents if selector is omitted.

You can read more about remove in Meteor docs.

Sign up to request clarification or add additional context in comments.

2 Comments

I get an error "this version of javascript is not supported", my Meteor installation has [email protected] and [email protected]. is your code written in ECMAScript 6?
Yes, I have used some ES6 for method declaration. I have edited the answer. This should prevent the error.
1

If I understand correctly, you need to seed data into FooterButtons collection, correct?

Put this code somewhere on your server folder:

buttonsList = ['NO', 'B', 'YES'];

if (FooterButtons.find().count() === 0) {
  _.each(buttonsList, function(button) {
    FooterButtons.insert({text: button});
  });
}

Run meteor and check your mongodb FooterButtons collection. Let me know. I'll make explanation if this work

If you need to update, then update it:

FooterButtons.update({text:'B'}, {$set:{text:'Extra'}});

1 Comment

Yes it does. But if I change the text of one of the items in the array (say B to Extra), the template does not show the new text

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.