Meteor,SimpleSchema,Meteor-collection2-添加到阵列中

Meteor,SimpleSchema,Meteor-collection2-添加到阵列中,meteor,simple-schema,meteor-collection2,Meteor,Simple Schema,Meteor Collection2,我已将我的文件和shema定义为: storageArticles: { type: Array, autoValue: function() { return []; }, label: 'Articless added to storage.', }, 'storageArticles.$': { type: String } 当我尝试使用(在我的服务器端方法)更新此字段时: 一切正常,但数据并没有添加到数组中 你能给我一些指导来解决这个问题吗 编辑 编辑添加了

我已将我的文件和shema定义为:

storageArticles:
{
  type: Array,
  autoValue: function() {
    return [];
  },
  label: 'Articless added to storage.',
},
'storageArticles.$': {
  type: String
}
当我尝试使用(在我的服务器端方法)更新此字段时:

一切正常,但数据并没有添加到数组中

你能给我一些指导来解决这个问题吗

编辑

编辑添加了更多关于这个问题的细节,也许这是我的错误

'articles.articleAddToStorage': function articleAddToStorage(storageId,articleId) {
    check(storageId, String);
    check(articleId, String);

    try {

      Articles.update(articleId, {  $set: {articleAssignedToStorage:true}});
      Storages.update(storageId , {$addToSet: { "storageArticles" : articleId }});

    } catch (exception) {
      handleMethodException(exception);
    }
  }

handleAddToStorage()
  {

    const articleId = this.props.articleId;
    const storageId = this.state.storageId;
    const history = this.props.history;

    if(storageId!="")
    {

      Meteor.call('articles.articleAddToStorage', storageId,articleId, (error) => {

        if (error) {
          Bert.alert(error.reason, 'danger');
        } else {

          Bert.alert("Article assigned to storage", 'success');

        }
      });
    }
    else {
    Bert.alert("Plese select storage", 'warning');

    }
  }
使用字段集操作符 排队

Storages.update(storageId , {$set: { "storageArticles" : articleId }});
基本上,您可以尝试将字符串值(articleId)添加到定义为字符串数组的字段中

只有将值数组设置为
storageArticles
(从而覆盖完整字段)时,这才有意义:

使用数组更新操作符 如果要推送或拉取值,可以查找(此处列出了一些示例):

$addToSet仅当元素不存在时才将其添加到数组中 在片场

$pop删除数组的第一项或最后一项

$pull删除与指定查询匹配的所有数组元素

$push将项目添加到数组中

$pullAll从数组中删除所有匹配的值

使用字段集操作符 排队

Storages.update(storageId , {$set: { "storageArticles" : articleId }});
基本上,您可以尝试将字符串值(articleId)添加到定义为字符串数组的字段中

只有将值数组设置为
storageArticles
(从而覆盖完整字段)时,这才有意义:

使用数组更新操作符 如果要推送或拉取值,可以查找(此处列出了一些示例):

$addToSet仅当元素不存在时才将其添加到数组中 在片场

$pop删除数组的第一项或最后一项

$pull删除与指定查询匹配的所有数组元素

$push将项目添加到数组中

$pullAll从数组中删除所有匹配的值


通过使用Storages.update(storageId,{$addToSet:{“storageArticles”:articleId}});我收到以下错误:更新失败:错误:存储项目必须是新文档或现有文档上的StringOn类型?我这样问是因为当你在之前将字段设置为articleId(字符串)的文档上尝试时,字段不再是数组了。对其使用数组更新方法可能会导致该错误。我打开了Robo 3T,我看到storageArticles的类型是数组,但当我尝试更新它时,它不起作用,我编辑了我的问题并提供了更多详细信息。尝试删除
自动值
-您不需要初始化空数组。谢谢@MichelFloyd,这与Jankapunkt先生的建议一起解决了这个问题;我收到以下错误:更新失败:错误:存储项目必须是新文档或现有文档上的StringOn类型?我这样问是因为当你在之前将字段设置为articleId(字符串)的文档上尝试时,字段不再是数组了。对其使用数组更新方法可能会导致该错误。我打开了Robo 3T,我看到storageArticles的类型是数组,但当我尝试更新它时,它不起作用,我编辑了我的问题并提供了更多详细信息。尝试删除
自动值
-您不需要初始化空数组。谢谢@MichelFloyd,这与Jankapunkt先生的建议一起解决了这个问题。
Storages.update(storageId , {$set: { "storageArticles" : [articleId] }});
Storages.update(storageId , {$addToSet: { "storageArticles" : articleId }});
Storages.update(storageId , { $pop : 1 });
Storages.update(storageId , {$pull: { "storageArticles" : articleId }});
Storages.update(storageId , {$push: { "storageArticles" : articleId }});
Storages.update(storageId , {$pullAll: { "storageArticles" : [articleId1, articleId2] }});