0

How is it possible to retrieve a particular element in an array as a string? I have populated a field called 'list' which consist of document id's from Posts collection.

The postId's are inserted in the following manner within my method:

var postId = //code for retrieving post id 
  Meteor.users.update( this.userId, { $addToSet: { 'profile.list': postId}});

Now, I retrieve the first postId value to find that Post document.

var postId = Meteor.user().profile.list;
Posts.update( {_id: postId}, { $addToSet: {'message': sometext}});

The update does not work as it appears in my console log I am retrieving an object [ 'SvNJAZWNFW8fpobJv' ] - not a string. If I were to manually insert the postId value enclosed with speech marks, it would work.

How can I target a specific position of the 'list' field and use the value to perform the update?

1 Answer 1

1

If you only want to update an individual postId::

// index is the position in the list you want to update (0 for the first one)
Posts.update( {_id: postId[index]}, { $addToSet: {'message': sometext}});

If you wanted to update the whole lot:

Posts.update( {_id: { $in: postId }}, { $addToSet: {'message': sometext}});
Sign up to request clarification or add additional context in comments.

2 Comments

Richsilv - thank you for the clear and concise answer.
What about retrieving a value from a particular position (0 in this example). I indexed the first array from the field 'likeids'which is stored within the users profile but unable to understand exactly what is going on. var likeid = Forms.find({_id: Meteor.user().profile.likeids[0]}).fetch();

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.