I'm trying to use the spread
method to accumulate promised results that I've read in this thread with Q.js
. It works in another block of code but not in the following app.get
example. I want to chain queries using Sequelize
and mongoose
and pass all the returned data to the spread method. Here's my attempt:
var db = require('./db/managedb'); // Sequelize
var mongo_models = require('./db/mongo_model')(mongoose);
var WB = mongo_models.Webdata,
Est = mongo_models.Estimate;
app.get('/p/:tagId', function(req, res){
var filename = req.param("tagId");
var mysql = db.db.query('CALL procedure()').then(function(rows) {
console.log(rows);
}); // Sequelize
var nosql = WB.find().exec(function(err,k){
console.log(k);
}) // Mongoose
var nosql2 = Est.find().exec(function(err,la){
console.log(la);
}) // Mongoose
Q.try(function(){
return mysql
}).then(function(mysqls){
return [ mysqls,nosql]
}).then(function(mysqls,nosqls){
return [mysqls,nosqls,nosql2]
}).spread(function(mysqls,nosqls,nosql2s){
res.render(filename+'.html', {my:mysqls,wb:nosqls,est:nosql2s})
}).catch(function(error){
console.log('fail')
})
})
I'm just getting a blank page with Cannot GET /p/5
and there's no "fail" shown in the console.log. Here's my original code that works, but it's suffering from callback hell.
app.get('/p/:tagId', function(req, res){
var filename = req.param("tagId");
db.db.query('CALL procedure()').then(function(rows) {
WB.find().exec(function(err,wb){
Est.find().exec(function(err,est){
res.render(filename+'.html', {my:rows,wb:wb,est:est})
})
})
}).catch(function (error) {
console.log('own: database error');
})
})