各位,我有个问题。在此模型中,我进行sql查询:
module.exports.contribId = (request, callback) => {
connection.query(
'SELECT * FROM planilla_produccion_cantera WHERE id_planilla_produccion_cantera = ?',
request,
(err, result) => {
var objPlanilla = {
id: result[0].id_planilla_produccion_cantera,
periodo: result[0].periodo
};
console.log('MY OBJ EN DAO: ', objPlanilla);
callback(null, objPlanilla);
}
);然后,在我的控制器中,我希望在一个映射中查找数组的次数,并使查询选择数组的次数到我的模型:
module.exports.contribId =(请求,回调) => { const = [];
const mapAsync = (arr, fn) => Promise.all(arr.map(fn));
mapAsync(request, id =>
contribModel.contribId(id, function(err, docs) {
todo.push(docs); //here I need to push each value
})
);
console.log(todo); //here I need to print all the values of mapsync}
但我无法使数组已经完成。你能帮我一下吗?
发布于 2018-10-13 15:32:24
试试这个:
在模型中只需回报承诺。
module.exports.contribId = (request) => {
return connection.query(
'SELECT * FROM planilla_produccion_cantera WHERE anilla_produccion_cantera = ?',
request
);
}在控制器中,收集承诺并使用Promise.all api,然后解析数组o结果。
module.exports.contribId = (request) => {
const todo = [];
const mapAsync = (arr, fn) => Promise.all(arr.map(fn));
mapAsync(request, id => contribModel.contribId(id))
.then((res) => {
console.log('here you can parse all your todos')
});
}https://stackoverflow.com/questions/52794333
复制相似问题