node.js - koa: 生成器不要下一步

node.js - koa: generator don't go next

我有一个简单的 object,我在其中传递一个参数,然后我想在我的 collections 中找到所有文档并标记它。这是我的习惯 object:

var db = require('../lib/db');
function Widget(n,l,c,o,t,p) {

    this.name = n;
    this.label = l;
    this.class = c;
    this.order = o;
    this.template = t;
    this.params = p;

    if(this.params != null) {
        var a = getValue(this.params);
        a.next();
     }
}
function *getValue(p){
    var Object = require("./sections");

    console.log("1");
    try {
        var objects = yield Object.find({}).exec();
    }
    catch(err){ 
        throw err;
    }
    console.log("2");
    console.log("obj:" + objects);
}
module.exports = Widget;

这是sections.js

var db = require('../lib/db');
var schema = new db.Schema(
{
    title:          String,
    parent:         { type: db.Schema.ObjectId, ref: 'sections' },
    child:          [{ type: db.Schema.ObjectId, ref: 'sections' }],
    updated_on:     Date,
    created_on:     Date
});
module.exports = db.model( 'sections', schema );

我这样创建 object:

var widget  = new Widget("text","Titiolo",0,0,"text.html","sections");

在我的控制台上我只看到“1”而不是 2 或者 "objects:" 为什么?

这与koa无关。这就是发电机的工作原理。 .next() 计算代码直到下一个 yield 函数。因为你的生成器函数中有 1 yield,你需要调用 .next() 两次来完成整个函数的计算,包括最后两个 console.log()s

此外,您的 try/catch 块没有任何作用。