【问题标题】:Meteor Collection.insert() not returning IdMeteor Collection.insert() 不返回 Id
【发布时间】:2018-07-13 14:00:06
【问题描述】:

我正在尝试获取新插入的 Id,以便可以将 Id 推送到另一个集合中。

根据这篇文章 => Meteor collection.insert callback to return new id 和这篇文章 => Meteor collection.insert callback issues,我应该可以

return Collection.insert(obj);

它会将新插入数据的 ID 返回给我的客户端。

相反,我得到了一个像这样的 Observable:

    {_isScalar: false}
    _isScalar: false
    __proto__:
       constructor: f Object()
       hasOwnProperty: f hasOwnProperty()
    //many other object properties

文档似乎很清楚,我应该得到 ID 作为回报。这是一个错误吗? https://docs.meteor.com/api/collections.html#Mongo-Collection-insert]

我的流星版本是 1.4.4.5... 我已经在这个问题上工作了几天,并且尝试了多种不同的方式来获取 ID,但我没有尝试任何结果。

这是我的完整代码供参考:

服务器:

submitStuff: function(data): string{
        var loggedInUser = Meteor.user();
        if (!loggedInUser || !Roles.userIsInRole(loggedInUser,['user','admin'])){
            throw new Meteor.Error("M504", "Access denied");
        } else {
            try{
                let insertedData = null;
                const model: MyModel[] = [{
                    data.stuff //bunch of data being inserted
                  }];
              model.forEach((obj: MyModel) => {
                insertedData = Collection.insert(obj);
              });
              return insertedData;
           } catch(e) {
                throw new Meteor.Error(e + e.reason, "|Throw new error|");
           }
        }
    },

客户:

Meteor.call('submitData', data, (error, value) => {
        if(error){
            this.errors.push(error + error.reason + "|new Error code|");
        }
        else{
            Meteor.call('userPushId', value, (error) => { //this pushes Id onto the second collection
                if(error){
                    this.errors.push(error + error.reason + "|new Error code|");
                }
            });
        }

【问题讨论】:

  • 您真的不需要/不应该将 Id 发送回客户端以写入另一个集合。您可以轻松地将写入内容放在第一个集合之后立即写入第二个集合。 Meteor 默认等待数据库调用,因此顺序写入的 dB 写入/读取顺序发生。
  • 是的,我试图/想要这样做。我只是在测试将它发送给客户.. 真的抓住了稻草
  • 所以问题是插入通常不返回 id?能否成功将id登录到服务器上的控制台?
  • 正确。我只能从 insert 方法中记录一个 Observable ......我也尝试在第二个参数中使用回调。回调不会将任何内容记录到控制台。
  • 哦。我想我明白了什么。当我参加比赛时,我会写一个解决方案。如果您将 foreach 更改为 map,您应该得到一个 id 数组。我会在一个小时左右的时间内进一步研究它

标签: javascript mongodb meteor observable angular-meteor


【解决方案1】:

服务器

// ...

try {
   const myModels = [{ ...whatever }];

   // I'm using map so I return an array of id's. 
   //your forEach about technically should end up with only one id,
   // which is the last insertion

   const insertedDataIds = myModels.map(model => Collection.insert(model));
  // you can also write to the secondary location here with something like:
  const secondaryWrite = SecondaryCollection.insert({ something: insertedDataIds });

  return insertedDataId's
}
//...

客户

我也不知道这是否只是堆栈上的拼写错误,但您的 Meteor.call('submitData') 应该是 Meteor.call('submitStuff') 但这可能不是您的实际问题。

【讨论】:

  • 我想知道这在 insert 方法的上下文中是如何工作的,该方法应该为插入到 mongodb 中的数据返回一个唯一的 Id。 .map 有自己的一组唯一 ID 吗?
  • 我在使用上述代码时控制台记录了一个未定义。
【解决方案2】:

好的,所以在四处搜索之后,我意识到我在创建集合时使用 angular-meteors rxjs 包来创建 MongoObservable,而不是 Mongo 的常规集合。

因为我正在使用 MongoObservable 并尝试在其上调用 .insert(),所以返回与常规的 Mongo 集合有点不同,并且将返回 Observable 而不是 Id。 Documentation Here

如果您在集合名称后添加.collection,您将获得 Id,如下所示:

submitStuff: function(data): string{
    var loggedInUser = Meteor.user();
    if (!loggedInUser || !Roles.userIsInRole(loggedInUser,['user','admin'])){
        throw new Meteor.Error("M504", "Access denied");
    } else {
        try{
            let id = new Mongo.ObjectID();
            const model: MyModel[] = [{
                data.stuff //bunch of data being inserted
                _id: 
              }];
          model.forEach((obj: MyModel) => {
            insertedData = Collection.collection.insert(obj); //added .collection
          });
          return insertedData;
       } catch(e) {
            throw new Meteor.Error(e + e.reason, "|Throw new error|");
       }
    }
},

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-17
    • 2017-12-04
    • 1970-01-01
    • 2018-03-04
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多