我正在尝试创建一个将从DB返回值的函数,然后我必须传递给res .render,我先使用回调函数来完成函数,然后再移至下一个,但是result
变量为空
router.get('/add_item', isAuthenticated, function(req, res, next) {
var result = userMerchantlist(req.session.userId);
res.render('merchants/add_item', {
merchants: result,
title: 'Add Item'
});
});
function userMerchantlist(uderid, callback){
db.merchants.findAll({
where: {
merchants_owner: uderid,
merchant_type: 'store',
}
})
.then((result) => {
if (result) {
callback(null,result);
}
});
};
答案 0 :(得分:0)
使用Promises,我自己解决了,这里的答案是代码
typedef struct{
int cmd;
int data[length]; //length is calculated in run time.
int len;
}rxBuf_s;
答案 1 :(得分:0)
这是您的解决方案,但有点麻烦:
router.get('/add_item', isAuthenticated, function (req, res, next) {
userMerchantlist(req.session.userId).then(result => {
if (result) {
res.render('merchants/add_item', {
merchants: result,
title: 'Add Item',
});
}
});
});
var userMerchantlist = uderid => {
return new Promise((resolve, reject) => {
db.merchants.findAll({
where: {
merchants_owner: uderid,
merchant_type: 'store',
}
})
.then(resolve)
.catch(reject)
});
};