Strongloop:未调用回送 beforeCreate
Strongloop : loopback beforeCreate not called
我有一个名为问题的模型,我在该模型上定义了 beforeCreate 和 beforeUpdate 方法来设置 createdAt 和 updatedAt timestamps.But 当我使用 api 资源管理器创建新问题时,它似乎从未被调用过。
这是我的 question.json:
{
"name": "question",
"plural": "questions",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"title": {
"type": "string",
"required": true
},
"body": {
"type": "string",
"required": true
},
"views": {
"type": "number"
},
"createdAt": {
"type": "date",
"required": true
},
"updatedAt": {
"type": "date",
"required": true
},
"id": {
"type": "number"
}
},
"validations": [],
"relations": {
"tags": {
"type": "hasMany",
"model": "tag",
"foreignKey": "question_id"
}
},
"acls": [],
"methods": []
}
和question.js
module.exports = function(question) {
question.beforeCreate = function (next, modelInstance) {
modelInstance.createdAt = Date.now();
modelInstance.updatedAt = Date.now();
next();
};
question.beforeUpdate = function (next, modelInstance) {
modelInstance.updatedAt = Date.now();
next();
};
};
创建时出现的错误是:
{
"error": {
"name": "ValidationError",
"status": 422,
"message": "The `question` instance is not valid. Details: `createdAt` can't be blank (value: undefined); `updatedAt` can't be blank (value: undefined).",
"statusCode": 422,
"details": {
"context": "question",
"codes": {
"createdAt": [
"presence"
],
"updatedAt": [
"presence"
]
},
"messages": {
"createdAt": [
"can't be blank"
],
"updatedAt": [
"can't be blank"
]
}
},
"stack": "ValidationError: The `question` instance is not valid. Details: `createdAt` can't be blank (value: undefined); `updatedAt` can't be blank (value: undefined).\n at /Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/dao.js:252:12\n at ModelConstructor.<anonymous> (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/validations.js:460:11)\n at ModelConstructor.next (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/hooks.js:75:12)\n at ModelConstructor.<anonymous> (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/validations.js:457:23)\n at ModelConstructor.trigger (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/hooks.js:65:12)\n at ModelConstructor.Validatable.isValid (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/validations.js:433:8)\n at /Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/dao.js:248:9\n at doNotify (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/model.js:641:49)\n at doNotify (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/model.js:641:49)\n at doNotify (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/model.js:641:49)"
}
}
我的输入是:
{
"title": "title",
"body": "body",
"views": 0
}
提前致谢
你能在question.js
中试试这个方法吗
module.exports = function(Question) {
Question.beforeRemote('create', function(context, user, next) {
var req = context.req;
req.body.createdAt = new Date(2014,2,2);
req.body.updatedAt = Date.now();
next();
});
};
模型挂钩已被弃用,取而代之的是操作挂钩:
http://docs.strongloop.com/display/public/LB/Operation+hooks
在您的情况下移植到操作挂钩的次数非常少:
module.exports = function(Question) {
Question.observe('before save', function(context, next) {
context.instance.createdAt = new Date(2014,2,2);
context.instance.updatedAt = Date.now();
next();
});
};
我有一个名为问题的模型,我在该模型上定义了 beforeCreate 和 beforeUpdate 方法来设置 createdAt 和 updatedAt timestamps.But 当我使用 api 资源管理器创建新问题时,它似乎从未被调用过。
这是我的 question.json:
{
"name": "question",
"plural": "questions",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"title": {
"type": "string",
"required": true
},
"body": {
"type": "string",
"required": true
},
"views": {
"type": "number"
},
"createdAt": {
"type": "date",
"required": true
},
"updatedAt": {
"type": "date",
"required": true
},
"id": {
"type": "number"
}
},
"validations": [],
"relations": {
"tags": {
"type": "hasMany",
"model": "tag",
"foreignKey": "question_id"
}
},
"acls": [],
"methods": []
}
和question.js
module.exports = function(question) {
question.beforeCreate = function (next, modelInstance) {
modelInstance.createdAt = Date.now();
modelInstance.updatedAt = Date.now();
next();
};
question.beforeUpdate = function (next, modelInstance) {
modelInstance.updatedAt = Date.now();
next();
};
};
创建时出现的错误是:
{
"error": {
"name": "ValidationError",
"status": 422,
"message": "The `question` instance is not valid. Details: `createdAt` can't be blank (value: undefined); `updatedAt` can't be blank (value: undefined).",
"statusCode": 422,
"details": {
"context": "question",
"codes": {
"createdAt": [
"presence"
],
"updatedAt": [
"presence"
]
},
"messages": {
"createdAt": [
"can't be blank"
],
"updatedAt": [
"can't be blank"
]
}
},
"stack": "ValidationError: The `question` instance is not valid. Details: `createdAt` can't be blank (value: undefined); `updatedAt` can't be blank (value: undefined).\n at /Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/dao.js:252:12\n at ModelConstructor.<anonymous> (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/validations.js:460:11)\n at ModelConstructor.next (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/hooks.js:75:12)\n at ModelConstructor.<anonymous> (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/validations.js:457:23)\n at ModelConstructor.trigger (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/hooks.js:65:12)\n at ModelConstructor.Validatable.isValid (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/validations.js:433:8)\n at /Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/dao.js:248:9\n at doNotify (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/model.js:641:49)\n at doNotify (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/model.js:641:49)\n at doNotify (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/model.js:641:49)"
}
}
我的输入是:
{
"title": "title",
"body": "body",
"views": 0
}
提前致谢
你能在question.js
module.exports = function(Question) {
Question.beforeRemote('create', function(context, user, next) {
var req = context.req;
req.body.createdAt = new Date(2014,2,2);
req.body.updatedAt = Date.now();
next();
});
};
模型挂钩已被弃用,取而代之的是操作挂钩:
http://docs.strongloop.com/display/public/LB/Operation+hooks
在您的情况下移植到操作挂钩的次数非常少:
module.exports = function(Question) {
Question.observe('before save', function(context, next) {
context.instance.createdAt = new Date(2014,2,2);
context.instance.updatedAt = Date.now();
next();
});
};