0

我有一个基于 koa 的应用程序,我想将数据写入 nedb。该问题显示在以下简短片段中。

app.use(router.get('/', function*(){
  db.insert(doc, function(err,data){
     // can't yield here because the callback is not a generator
  }
}));

根据https://github.com/tj/node-thunkify上的文档,我使用 thunkify 尝试了以下操作:

var insert = thunkify(db.insert);
app.use(router.get('/', function* (){
   yield insert(doc)
}))

但我收到以下错误

TypeError: Cannot read property 'push' of undefined
  at Datastore.insert (/home/app/node_modules/nedb/lib/datastore.js:374:16)
  at Object.<anonymous> (/home/app/node_modules/thunkify/index.js:43:12)
  at /home/app/node_modules/koa/node_modules/co/index.js:136:8
  at Object.thunkToPromise (/home/app/node_modules/koa/node_modules/co/index.js:13

任何帮助将不胜感激。

4

1 回答 1

0

我发现有https://www.npmjs.com/package/co-nedb它也使用 thunkify 但可以工作:-)。

于 2015-10-02T20:57:26.843 回答