简体   繁体   English

从流星中的数组中以字符串形式检索值

[英]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. 我填充了一个名为“列表”的字段,其中包含Posts集合中的文档ID。

The postId's are inserted in the following manner within my method: postId以以下方式插入到我的方法中:

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. 现在,我检索第一个postId值以查找该Post文档。

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. 由于我正在检索对象[ 'SvNJAZWNFW8fpobJv' ] -不是字符串,因此更新不起作用,因为它出现在控制台日志中。 If I were to manually insert the postId value enclosed with speech marks, it would work. 如果我要手动插入带有语音标记的postId值,那么它将起作用。

How can I target a specific position of the 'list' field and use the value to perform the update? 如何定位“列表”字段的特定位置并使用该值执行更新?

If you only want to update an individual postId:: 如果您只想更新单个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}});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM