e-oj / Fawn

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

Can't access the _id property in subsequent step #49

Closed VinayVi closed 6 years ago

VinayVi commented 6 years ago

Here is my code

var task = fawn.Task();
    task.save('Group', group)
        .update(User, { _id: req.user._id }, { $push: { groups: { $ojFuture: "0._id" } } })
        .run({ useMongoose: false }).then(function (results) {
            console.log(results[0]);
            res.json(group);
        }).catch(function (err) {
            console.log(err);
            next(err);
        });

On the update line, I call {$odFuture: "0._id"}, exactly how its done in the tutorial, but this produces the error

Error: No such key exists in result {"n":1,"ok":1} at index0
    at C:\Users\Vinay\Projects\express_api_learning\node_modules\fawn\lib\utils\gen.utils.js:167:15
    at Array.forEach (native)
    at exports.resolveFuture (C:\Users\Vinay\Projects\express_api_learning\node_modules\fawn\lib\utils\gen.utils.js:143:20)
    at C:\Users\Vinay\Projects\express_api_learning\node_modules\fawn\lib\utils\gen.utils.js:145:32
    at Array.forEach (native)
    at exports.resolveFuture (C:\Users\Vinay\Projects\express_api_learning\node_modules\fawn\lib\utils\gen.utils.js:143:20)
    at Object.exports.nativeUpdate (C:\Users\Vinay\Projects\express_api_learning\node_modules\fawn\lib\task_core\native.js:36:3)
    at performUpdate (C:\Users\Vinay\Projects\express_api_learning\node_modules\fawn\lib\task.js:359:14)
    at C:\Users\Vinay\Projects\express_api_learning\node_modules\fawn\lib\task.js:304:38
    at tryCatcher (C:\Users\Vinay\Projects\express_api_learning\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (C:\Users\Vinay\Projects\express_api_learning\node_modules\bluebird\js\release\promise.js:512:31)
    at Promise._settlePromise (C:\Users\Vinay\Projects\express_api_learning\node_modules\bluebird\js\release\promise.js:569:18)
    at Promise._settlePromise0 (C:\Users\Vinay\Projects\express_api_learning\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (C:\Users\Vinay\Projects\express_api_learning\node_modules\bluebird\js\release\promise.js:693:18)
    at Async._drainQueue (C:\Users\Vinay\Projects\express_api_learning\node_modules\bluebird\js\release\async.js:133:16)
    at Async._drainQueues (C:\Users\Vinay\Projects\express_api_learning\node_modules\bluebird\js\release\async.js:143:10)

Not sure what I'm doing wrong here.

e-oj commented 6 years ago

That's happening because you're accessing the raw MongoDB result. use: {$ojFuture: "0.ops.0._id"}

e-oj commented 6 years ago

Actually, the mongodb result of a save is {n: numAffected, ok: 1or0} not a document. In your case, that's {"n":1,"ok":1}

e-oj commented 6 years ago

I'll be closing this issue. Feel free to reopen it if you're still having problems.

e-oj commented 6 years ago

You should check out the results reference before using the template

VinayVi commented 6 years ago

So I decided to set useMongoose as true, so that I could access the _id feature.

Use mongoose.model(name, schema)
    at MissingSchemaError (C:\Users\Vinay\Projects\express_api_learning\node_modules\fawn\node_modules\mongoose\lib\error\missingSchema.js:20:11)
    at Mongoose.model (C:\Users\Vinay\Projects\express_api_learning\node_modules\fawn\node_modules\mongoose\lib\index.js:391:13)
    at getCollection (C:\Users\Vinay\Projects\express_api_learning\node_modules\fawn\lib\utils\gen.utils.js:268:16)
    at rollbackSave (C:\Users\Vinay\Projects\express_api_learning\node_modules\fawn\lib\roller.js:135:20)
    at C:\Users\Vinay\Projects\express_api_learning\node_modules\fawn\lib\roller.js:97:39
    at tryCatcher (C:\Users\Vinay\Projects\express_api_learning\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (C:\Users\Vinay\Projects\express_api_learning\node_modules\bluebird\js\release\promise.js:512:31)
    at Promise._settlePromise (C:\Users\Vinay\Projects\express_api_learning\node_modules\bluebird\js\release\promise.js:569:18)
    at Promise._settlePromiseCtx (C:\Users\Vinay\Projects\express_api_learning\node_modules\bluebird\js\release\promise.js:606:10)
    at Async._drainQueue (C:\Users\Vinay\Projects\express_api_learning\node_modules\bluebird\js\release\async.js:138:12)
    at Async._drainQueues (C:\Users\Vinay\Projects\express_api_learning\node_modules\bluebird\js\release\async.js:143:10)
    at Immediate.Async.drainQueues (C:\Users\Vinay\Projects\express_api_learning\node_modules\bluebird\js\release\async.js:17:14)
    at runCallback (timers.js:781:20)
    at tryOnImmediate (timers.js:743:5)
    at processImmediate [as _immediateCallback] (timers.js:714:5)
  message: 'Schema hasn\'t been registered for model "Group".\nUse mongoose.model(name, schema)',
  name: 'MissingSchemaError' }

This is the response I get when that happens, which is weird because I register the group schema earlier

module.exports = global.mongoose.model('Group', GroupSchema);

Thats in a seperate file that imported into the fawn routes file.

VinayVi commented 6 years ago

I also tried using mongoose regularly instead of setting it as a global variable.

fawn.init(global.mongoose);

This is how i initialize fawn.

e-oj commented 6 years ago

This issue has been addressed (#46). Upgrade fawn to 2.1.4

htooyannaing commented 6 years ago

Hello Mr. @e-oj I don't understand what the meaning of 'accessing the raw MongoDB'. Could you explain me?

e-oj commented 6 years ago

By default, Fawn uses the MongoDB native driver to run all tasks.

// If you run the task like this
task.run()
// you get the result from the native driver (the raw MongoDB result)

//To get the mongoose result, run it like this:
task.run({useMongoose: true});

You can get more information about the "run" function from the docs