首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用async await构建api

使用async await构建api
EN

Stack Overflow用户
提问于 2020-03-10 18:39:22
回答 1查看 46关注 0票数 0

我已经用express写了一个Node api。在这个api中,我从数据库中获取一些数据,对其进行处理,并将最终数据作为api有效负载发送。

这是我的代码..

代码语言:javascript
复制
exports.getAllAco =  (req, res) => {
    let acoList = [], i = 0;

    let condaions = {
        deleted: 0,
    }

    let attributes = ['id', 'name', 'email', 'type', 'phone', 'gender', 'main_sys_id', 'avatar', 'in_service', 'created_by']
    Methods.getAllData(User, condaions, attributes).then((userList) => {
            userList.map( user => {
                i = i + 1;
                if (user.dataValues.type == 'aco') {
                    let condations = {
                        aquisition_member_id: user.dataValues.id,
                        deleted: 0
                    }

                    Methods.getDetailsFromTwoAssociateTable(condations, Task, DetailTask).then(tasks => {
                        let x = {
                            user: user,
                            tasks: tasks
                        }

                        x.user.dataValues.totalTask = tasks.length;

                        let index = 0;

                        x.user.dataValues.userTotalAssigned = 0, x.user.dataValues.userTotalCalled = 0, x.user.dataValues.userTotalConverted = 0, x.user.dataValues.userTotalRejected = 0;

                        tasks.map(v=>{
                            x.tasks[index].dataValues.totalAssignedCustomer = v.dataValues.detail_tasks.length;
                            x.tasks[index].dataValues.totalCalled = 0;
                            x.tasks[index].dataValues.totalConverted = 0;
                            x.tasks[index].dataValues.totalRejected = 0;
                            v.dataValues.detail_tasks.map(detail_task=>{

                                 x.user.dataValues.userTotalAssigned += 1;

                                if(detail_task.dataValues.phone_call_status == 'called' ){
                                    x.tasks[index].dataValues.totalCalled += 1;
                                    x.user.dataValues.userTotalCalled +=1;
                                }

                                else if(detail_task.dataValues.phone_call_status == 'confirmed'){
                                    x.tasks[index].dataValues.totalConverted +=1;
                                    x.user.dataValues.userTotalConverted += 1;
                                }

                                else{
                                    x.user.dataValues.userTotalRejected += 1;
                                    x.tasks[index].dataValues.totalRejected += 1;
                                }
                            })
                            index=index+1;
                        })

                        // x.user.dataValues.totalAssigned = totalAssigned;

                        acoList.push(x)                           
                    })
                }
            })
            return acoList;
    }).then(acoList=>{
       setTimeout(() => {
        Methods.successResponse(req, res, acoList)
       }, 2000);
    }).catch((error) => {
        ErrorResMethods.errorResponse(req, res, error);
    })
}

要发送实际结果,我必须等待两秒钟,否则响应为空。

我如何使用async/await来实现它。

EN

回答 1

Stack Overflow用户

发布于 2020-03-10 18:51:35

我想像这样的东西应该可以完成工作。

代码语言:javascript
复制
exports.getAllAco = async (req, res) => {
  let i = 0;

  const condaions = {
    "deleted": 0
  };

  const attributes = ["id", "name", "email", "type", "phone", "gender", "main_sys_id", "avatar", "in_service", "created_by"];
  const userList = await Methods.getAllData(User, condaions, attributes);
  const acoList = await Promise.all(userList.map(async user => {
    i += 1;
    if (user.dataValues.type == "aco") {
      const condations = {
        "aquisition_member_id": user.dataValues.id,
        "deleted": 0
      };

      const tasks = await Methods.getDetailsFromTwoAssociateTable(condations, Task, DetailTask);
      const x = {
        "user": user,
        "tasks": tasks
      };

      x.user.dataValues.totalTask = tasks.length;

      let index = 0;

      x.user.dataValues.userTotalAssigned = 0, x.user.dataValues.userTotalCalled = 0, x.user.dataValues.userTotalConverted = 0, x.user.dataValues.userTotalRejected = 0;

      tasks.forEach(v => {
        x.tasks[index].dataValues.totalAssignedCustomer = v.dataValues.detail_tasks.length;
        x.tasks[index].dataValues.totalCalled = 0;
        x.tasks[index].dataValues.totalConverted = 0;
        x.tasks[index].dataValues.totalRejected = 0;
        v.dataValues.detail_tasks.map(detail_task => {

          x.user.dataValues.userTotalAssigned += 1;

          if (detail_task.dataValues.phone_call_status == "called") {
            x.tasks[index].dataValues.totalCalled += 1;
            x.user.dataValues.userTotalCalled += 1;
          } else if (detail_task.dataValues.phone_call_status == "confirmed") {
            x.tasks[index].dataValues.totalConverted += 1;
            x.user.dataValues.userTotalConverted += 1;
          } else {
            x.user.dataValues.userTotalRejected += 1;
            x.tasks[index].dataValues.totalRejected += 1;
          }
        });
        index += 1;
      });
      return x;
    }
  })
  Methods.successResponse(req, res, acoList);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60615912

复制
相关文章

相似问题

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