meteorBuzz
meteorBuzz

Reputation: 3200

Retrieve value as string from an array in Meteor

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?

Upvotes: 0

Views: 97

Answers (1)

richsilv
richsilv

Reputation: 8013

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}});

Upvotes: 1

Related Questions