【发布时间】:2017-12-01 12:36:39
【问题描述】:
我的代码:
return Doc.createReadStream({
where: { /*...*/ },
include: [
{
/*...*/
},
],
})
.pipe(through({ objectMode: true }, (doc, enc, cb) => {
Comment.findOne(null, { where: { onId: doc.id } }).then((com) => { /* sequelize: findOne*/
com.destroy(); /* sequelize instance destroy: http://docs.sequelizejs.com/manual/tutorial/instances.html#destroying-deleting-persistent-instances */
cb();
});
}))
.on('finish', () => {
console.log('FINISHED');
})
.on('error', err => console.log('ERR', err));
我试图清楚地表达我的问题。 Doc 和 Comment 是续集模型。我想使用流从数据库中一个一个地读取 Doc 实例,并删除每个 Doc 实例上的 cmets。 Comment.findOne 和 com.destroy() 都将返回承诺。我想为每个doc 解决承诺,然后调用cb()。但是我上面的代码不能工作,在com被销毁之前,代码已经运行完毕了。
如何解决?谢谢
我把上面这段代码包装在mocha测试中,比如
it('should be found by readstream', function _testStream(){
/* wrap the first piece of codes here*/
});
但在流读完之前,测试已经存在。
【问题讨论】:
标签: node.js sequelize.js node-streams through2