Sails.js afterCreate()在没有实例方法的情况下传递了newlyCreatedRecord

时间:2016-05-16 08:54:27

标签: sails.js waterline

afterCreate(newlyInsertedRecord, cb) {}中的newInsertedRecord缺少所有实例方法。

  1 /**
  2  * Dog.js
  3  *
  4  * @description :: TODO: You might write a short summary of how this model works and what it represents here.
  5  * @docs        :: http://sailsjs.org/documentation/concepts/models-and-orm/models
  6  */
  7 
  8 module.exports = {
  9 
 10   attributes: {
 11     tail: 'string',                                                                                                                                                                                             
 12     wag: function() {
 13       console.log('wagging ', this.tail, 'tail')
 14     }
 15   },
 16 
 17   afterCreate: function(dog, cb) {
 18     dog.wag()
 19   }
 20 };

执行Dog.create({tail:'green'}).exec(console.log)时,会发生以下错误和堆栈跟踪:

sails> TypeError: dog.wag is not a function
    at module.exports.afterCreate (/tmp/afterCreateTest/api/models/Dog.js:18:7)
    at fn (/home/user/.npm-packages/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/callbacksRunner.js:60:10)
    at /home/user/.npm-packages/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:181:20
    at iterate (/home/user/.npm-packages/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:262:13)
    at Object.async.forEachOfSeries.async.eachOfSeries (/home/user/.npm-packages/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:281:9)
    at Object.async.forEachSeries.async.eachSeries (/home/user/.npm-packages/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:214:22)
    at Object.runner.afterCreate (/home/user/.npm-packages/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/callbacksRunner.js:63:9)
    at after (/home/user/.npm-packages/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/dql/create.js:243:17)
    at /home/user/.npm-packages/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/dql/create.js:230:68
    at wrapper (/home/user/.npm-packages/lib/node_modules/sails/node_modules/lodash/index.js:3592:19)
    at applyInOriginalCtx (/home/user/.npm-packages/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:421:80)
    at wrappedCallback (/home/user/.npm-packages/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:324:18)
    at callback.success (/home/user/.npm-packages/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/normalize.js:33:31)
    at _switch (/home/user/.npm-packages/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/factory.js:58:28)
    at afterwards (/home/user/.npm-packages/lib/node_modules/sails/node_modules/waterline/lib/waterline/adapter/dql.js:89:19)
    at wrapper (/home/user/.npm-packages/lib/node_modules/sails/node_modules/lodash/index.js:3592:19)

不确定Sails.js model saved to session loses custom instance methods?的答案是否适用?

1 个答案:

答案 0 :(得分:1)

afterCreate()生命周期回调仅返回数据库中添加的值,即JSON。例如:

{
    tail: "green",
    createdAt: "...",
    updatedAt: "...",
    id: "173c69cbdc4810b70dd4fb73"
}

此时你只能访问JSON,而不是使用方法的对象。