简体   繁体   中英

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

I have a simple object where I pass a parameter, then I want to find all document in my collections and stamp it. This is my custom 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;

and this is 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 );

I create the object in this way:

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

on my console I see only "1" not 2 or "objects:" why?

this has nothing to do with koa . this is just how generators work. .next() evaluates the code up until the next yield function. because you have 1 yield in your generator function, you'll need to call .next() twice to finish evaluating the entire function including the last two console.log() s

also, your try/catch block doesn't do anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM