Reputation: 1721
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?
Upvotes: 1
Views: 73
Reputation: 20315
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.
Upvotes: 1