javascript - Error on Bluebird spread function -


when start code

import promise 'bluebird'; const mongodb = promise.promisifyall(require('mongodb')); const mongoclient = mongodb.mongoclient; mongoclient.connect(url).then((db) => {     return promise.all([new workerservice(db)]); }).spread((workerservice) => (     promise.all([new workerrouter(workerservice)]) )).spread((workerrouter) => {    app.use('/worker', workerrouter); }).then(() => {     httpserver.start(config.get('server.port')); }).catch((err) => {     console.log(err);    httpserver.finish(); 

});

i see error

}).spread(function (workerservice) {    ^  typeerror: mongoclient.connect(...).then(...).spread not function 

please me. doing wrong?

i see several things wrong here:

the root cause here mongoclient.connect(...).then(...) not returning bluebird promise, thus, there no .spread() method. when promisify interface bluebird, not change existing methods @ all. instead, adds new methods "async" suffix on it.

so, unless you've somehow told mongo create bluebird promises,

`mongoclient.connect(...).then(...)` 

will returning whatever kind of promise mongo has built in. need use new promisified methods:

mongoclient.connectasync(...).then(...).spread(...) 

some other issues see in code:

1) promise.all() expects pass array of promises. when this: promise.all([new workerrouter(workerservice)]), you're passing array of single object unless object promise itself, wrong.

2) in code:

}).spread((workerservice) => (     promise.all([new workerrouter(workerservice)]) )) 

you need return resulting promise in order link chain:

}).spread((workerservice) => (     return promise.all([new workerrouter(workerservice)]) )) 

3) but, there's no reason use promise.all() on single element array. if promise, return it.

4) looks you're trying use promises different steps of synchronous code too. besides complicating heck out of things, there's no reason this:

}).spread((workerservice) => (     promise.all([new workerrouter(workerservice)])     )).spread((workerrouter) => {    app.use('/worker', workerrouter); }).then(() => {     httpserver.start(config.get('server.port')); })... 

can combined this:

)).spread((workerservice) => {     app.use('/worker', new workerrouter(workerservice));     httpserver.start(config.get('server.port')); })... 

and, can condensed further since new workerservice(db) doesn't return promise.

5) and, can't tell why you're attempting use .spread() in first place. useful when have array of results want turn multiple named arguments, don't need have array of results (since there no reason use promise.all() single item , in modern version of node.js, there's no reason use .spread() more because can use es6 destructing of array function argument accomplish same thing regular .then().


i don't claim follow you're trying every line, may able this:

import promise 'bluebird'; const mongodb = promise.promisifyall(require('mongodb')); const mongoclient = mongodb.mongoclient; mongoclient.connectasync(url).then((db) => {     let service = new workerservice(db);     app.use('/worker', new workerrouter(service));     // since httpserver.start() async , there's no promise returning here,     // promise chain not wait server start     httpserver.start(config.get('server.port')); }).catch((err) => {     console.log(err);     httpserver.finish(); }); 

Comments