node.js - Type error when chaining Q.ninvoke -


there error when trying use q chain mongodb functions in node.js follow:

q.ninvoke(mongoclient, 'connect', 'mongodb://127.0.0.1:27017/mydb') .then(function(db){     return q     .ninvoke(db, 'createcollection', 'mycollection')     .ninvoke(db.collection('mycollection'), 'createindex', {id: 1}) // error occurs here     .ninvoke(db, 'close')     .then(function(){...}) }); 

the error message got:

typeerror: cannot call method 'apply' of undefined   @ promise.post (/path/to/my/project/node_modules/q/q.js:1157:36)   @ promise.promise.promisedispatch (/path/to/my/project/node_modules/q/q.js:784:41)   @ /path/to/my/project/node_modules/q/q.js:600:44   @ runsingle (/path/to/my/project/node_modules/q/q.js:133:13)   @ flush (/path/to/my/project/node_modules/q/q.js:121:13)   @ process._tickcallback (node.js:442:13) 

according message, line 1157 in q.js about:

q.fulfill = fulfill; function fulfill(value) {     return promise({     ...     "post": function (name, args) {         if (name === null || name === void 0) {             return value.apply(void 0, args);         } else {             return value[name].apply(value, args); // error occurs here: line 1157         }     } 

although have little idea what's going on inside q, guess db.collection('mycollection') not passed correctly value in line 1157. i've raised issue on github of q haven't got response.

if change code this, works fine again:

q.ninvoke(mongoclient, 'connect', 'mongodb://127.0.0.1:27017/mydb') .then(function(db){     return q     .ninvoke(db, 'createcollection', 'mycollection')     .then(function(){         return q.ninvoke(db.collection('mycollection'), 'createindex', {id: 1}) // no error time         .then(function(){              return q.ninvoke(db, 'close').then(function(){...});         });     }); }); 

however, here comes pyramid grows along chain. think q should have supported chaining ninvoke first example.

in short, question whether there misunderstanding of use q, or there bug in q?

package versions used: node.js: v0.10.36 q: 1.4.0 mongodb: 2.0.31

update

i rule out factor of mongodb , narrow down scope of problem follow:

var testclass = function (name){ };  testclass.prototype.printname = function (callback) {     console.log('printname called');     return callback(null); };  var test = new testclass('test object');  test.printname(function (err) {     test.printname(function (err) {         console.log('callback called');     }); }); 

in case, output should be:

$ node q-test.js printname called printname called callback called 

but if use q follow:

q.ninvoke(test, 'printname') .ninvoke(test, 'printname') .then(function(){     console.log('callback called'); }) .done(); 

it turns out output error this:

$ node test.js printname called  /path/to/my/project/node_modules/q/q.js:155                 throw e;                       ^ typeerror: cannot read property '[object object]' of undefined     @ promise.post (/path/to/my/project/node_modules/q/q.js:1161:29)     @ promise.promise.promisedispatch (/path/to/my/project/node_modules/q/q.js:788:41)     @ /path/to/my/project/node_modules/q/q.js:556:49     @ runsingle (/path/to/my/project/node_modules/q/q.js:137:13)     @ flush (/path/to/my/project/node_modules/q/q.js:125:13)     @ process._tickcallback (node.js:442:13)     @ function.module.runmain (module.js:499:11)     @ startup (node.js:119:16)     @ node.js:929:3 

tl;dr version: when called in chain opposed directly q.ninvoke() object function invoked comes result of prior function in chain, not first parameter qpromise.ninvoke() call.

elaboration: while thought quick glance issue in original code, collection not yet have been created when db.collection('mycollection') called in following line. in correct/working version, addressed, because ninvoke binding isn't happening until after collection has been created. see general problem how calling .ninvoke() past initial use in chain.

your updated example wasn't directly helpful, because using ninvoke() not using node-style callback form it, bound different sort of errors.

at least referring new non-mongo example root problem how using .ninvoke() in subsequent calls. after initial q.ninvoke() subsequent .ninvoke() calls when applied returned promise, uses return value of promise first parameter. maybe more illustrated else, please review change made "printname" example:

var testclass = function (name){ };  testclass.prototype.printname = function (callback) {     console.log('printname called');     return callback(null,this);     // node style callback, return };  var test = new testclass('test object');  q.ninvoke(test, 'printname') //  in lines below, result of prior line "implied" first // parameter ninvoke(); .ninvoke('printname') .ninvoke('printname')    .then(function(){     console.log('callback called'); }) .done(); 

hopefully illustrates going on.

returning original quesiton, while not mongo user, looking @ docs it, appears createcollection in fact returns collection, amend earlier code:

return q .ninvoke(db, 'createcollection', 'mycollection') .ninvoke(db.collection('mycollection'), 'createindex', {id: 1}) // error occurs here 

into

return q .ninvoke(db, 'createcollection', 'mycollection') .ninvoke('createindex', {id: 1}) // error *should not occur* here :-) 

however, not sure want "close" since presumably createindex not return reference db. don't know api presumably use q's .get() or related functions , pass along .ninvoke('close').

see this issue discussion of implementation in q, has link changes made implement , can see how working.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -