Javascript Meteor Collection.insert()未返回Id

Javascript Meteor Collection.insert()未返回Id,javascript,mongodb,meteor,observable,angular-meteor,Javascript,Mongodb,Meteor,Observable,Angular Meteor,我正在尝试获取新插入的Id,以便将该Id推送到另一个集合上 根据这篇文章=> 这个帖子=>,我应该可以 return Collection.insert(obj); 它会将新插入数据的ID返回给我的客户机 相反,我得到的是这样一个可见光: {_isScalar: false} _isScalar: false __proto__: constructor: f Object() hasOwnProperty: f hasOwnProperty

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

根据这篇文章=> 这个帖子=>,我应该可以

return Collection.insert(obj);
它会将新插入数据的ID返回给我的客户机

相反,我得到的是这样一个可见光:

    {_isScalar: false}
    _isScalar: false
    __proto__:
       constructor: f Object()
       hasOwnProperty: f hasOwnProperty()
    //many other object properties
文档似乎很清楚,我应该得到ID作为回报。这是虫子吗

我的流星版本是1.4.4.5。。。 我已经在这个问题上工作了几天,我尝试了许多不同的方法来获取ID,但我尝试的任何方法都不会导致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|");
                }
            });
        }
客户端:

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

服务器

// ...

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('submitsttuff')
,但这可能不是你的实际问题。

好的,因此,在四处搜索之后,我意识到我正在使用angular meteors rxjs软件包创建一个
MongoObservable
来创建一个集合,而不是Mongo的常规集合

因为我使用的是
MongoObservable
并试图调用
.insert()
,因此返回值与常规Mongo集合略有不同,将返回一个可观察值,而不是Id

如果在集合名称后添加一个
.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|");
       }
    }
},

您真的不需要/不应该将I'd发送回客户机以写入另一个集合。您可以很容易地将写操作放在第一个集合之后的第二个集合中。Meteor默认情况下会等待数据库调用,所以按顺序写入的dB写入/读取会按顺序进行。是的,我正试图/想要这样做。我只是试着把它寄给客户。。真正理解问题的是,insert通常不返回id?您能否成功地将id登录到服务器上的控制台?正确。我只能通过插入方法记录一个可观察到的。。。我也尝试在第二个参数中使用回调。回调不会将任何内容记录到控制台。我想我明白了。我到公司后会写一份解决方案。如果您将foreach更改为map,则应该获得一个id数组。我将在一个小时或更长的时间内对此进行更多的研究,我想知道这在insert方法的上下文中是如何工作的,insert方法应该为插入mongodb的数据返回一个唯一的Id。map是否有自己的一组唯一Id?在使用上述代码时,我控制台记录了一个未定义的Id。