e-oj / Fawn

Transactions for MongoDB (See the README)
https://www.npmjs.com/package/fawn
MIT License
486 stars 54 forks source link

How to retrieve values returned by a task.save()? #64

Closed amitschandillia closed 6 years ago

amitschandillia commented 6 years ago

This is my implementation of Fawn in my project. I am using it to take an input object and add it to one collection as a new document and then update another collection with some of its values:

var task = fawn.Task();
      task.save(
        dbPost,
        {
          _id: new mongoose.Types.ObjectId(),
          title: args.title,
          content: args.content,
          author: {
            id: args.author_id,
            first_name: args.author_first_name,
            last_name: args.author_last_name,
          }
        }
      );
      task.update(
        dbUser,
        { _id: args.author_id },
        {$push: {posts:
          {
            id: {$ojFuture: "0._id"},
            title: args.title,
            content: args.content,
          }
        }}
      );
      task.run({useMongoose: true})
      .then(function(){
        // task is complete
        console.log('done');
        // value returned by task.save()
        console.log('results');
      })
      .catch(function(err){
        // Everything has been rolled back.
        console.log(err);

Here, in the .then() block, I somehow need access to the data returned by task.save() (where it says console.log('results')). How can I do this? console.log($ojFuture) obviously returns an undefined error.

amitschandillia commented 6 years ago

The following lines worked:

const results = await task.run({useMongoose: true});
return results[0];