简体   繁体   中英

Meteor Collection.insert() not returning Id

I'm trying to get the Id of the new insert so I can push the Id onto another collection.

According to this post => Meteor collection.insert callback to return new id and this post => Meteor collection.insert callback issues , I should be able to

return Collection.insert(obj);

and it will return the ID of the newly inserted data to my client.

Instead I'm getting an Observable like this:

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

The documentation seems pretty clear that I should be getting the ID in return. Is this a bug? https://docs.meteor.com/api/collections.html#Mongo-Collection-insert]

My version of meteor is 1.4.4.5... I've been working on this issue for a few days and I've tried getting the ID many different ways, nothing I've tried results in the ID.

Here's my full code for reference:

Server:

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

Client:

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

Server

// ...

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
}
//...

Client

also I don't know if this is just a typo on stack but your Meteor.call('submitData') should be Meteor.call('submitStuff') but that is probably not your actual issue.

Alright, so after searching around I realized I'm using angular-meteors rxjs package to create a MongoObservable when creating a collection instead of Mongo's regular collection.

Because I'm using MongoObservable and trying to invoke .insert() on that, the return is a bit different then a regular Mongo Collection and will return an Observable instead of the Id. Documentation Here

If you add a .collection after the collection name you will get the Id in return like so:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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