Node.js 膝关节炎反应误差水线
我正在使用水线ORM在mongo DB中插入一个新的用户数据。这是我的控制器操作中的代码Node.js 膝关节炎反应误差水线,node.js,sails.js,waterline,koa,Node.js,Sails.js,Waterline,Koa,我正在使用水线ORM在mongo DB中插入一个新的用户数据。这是我的控制器操作中的代码 function *(){ var self = this; var attributes= this.request.body var userModel = this.models.user; userModel.create(attributes).exec(function(err, model){ self.type = 'application/
function *(){
var self = this;
var attributes= this.request.body
var userModel = this.models.user;
userModel.create(attributes).exec(function(err, model){
self.type = 'application/json';
self.body = {success:true,description:"user Created"};
});
}
当我尝试执行请求时,出现以下错误:
...../node_modules/sails-mongo/node_modules/mongodb/lib/mongodb/connection/base.js:245
throw message;
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:331:11)
我不是Koa的专家,但我想这是因为这是一个异步的过程,答案写在前面。
有人能帮我吗??我很有兴趣了解这项技术
我认为这是因为这是一个异步过程,而应答消息是以前编写的
嗯。Koa不知道查询,所以它在查询执行前继续进行响应。
您可以将代码>收益> /COD> <代码>函数>代码>膝关节炎,它包装查询并通知它完成。< /P>
function *() {
// ...
yield function (done) {
userModel.create(attributes).exec(function (err, model) {
self.type = 'application/json';
self.body = {success:true,description:"user Created"};
done(err);
});
}
}
在中进一步演示了此类thunks的使用,其中使用生成此类函数