首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获得这些承诺的价值?

如何获得这些承诺的价值?
EN

Stack Overflow用户
提问于 2018-10-13 15:08:02
回答 1查看 46关注 0票数 0

各位,我有个问题。在此模型中,我进行sql查询:

代码语言:javascript
复制
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 = [];

代码语言:javascript
复制
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

}

但我无法使数组已经完成。你能帮我一下吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-13 15:32:24

试试这个:

在模型中只需回报承诺。

代码语言:javascript
复制
  module.exports.contribId = (request) => {
  return connection.query(
    'SELECT * FROM planilla_produccion_cantera WHERE anilla_produccion_cantera = ?',
    request
  );
}

在控制器中,收集承诺并使用Promise.all api,然后解析数组o结果。

代码语言:javascript
复制
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')
    });
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52794333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档