簡體   English   中英

我正在使用異步瀑布並遇到一些問題

[英]I am using async-waterfall and facing some issues

我只是想從名為“ slt_timesheet”的文檔中獲取詳細信息。 我正在使用async-waterfall在序列中運行2-3函數。

如果我使用“ get”方法獲取slt_timesheet的詳細信息,那么我會獲得有關slt-timesheet的所有詳細信息,但是如果我使用async-waterfall,則無法從文檔中獲取詳細信息。

    const express = require('express');
    const app=express();

    const async=require("async");
    const waterfall=require('async-waterfall');

    const TIMESHEET_DETAILS = require('../Models/slt_timesheet');

    //For filing the timesheet
          router.post('/postTimesheet',(req,res)=>{
            let monthFromClient=req.body.month;
            let projectArrayFromClient=req.body.projectArray;
            let activityArray=req.body.activityArray;
            let hoursArrayForAMonth=req.body.hoursArray;

            let currentDateObject = new Date();
            let lastMonthDateObject= new Date(currentDateObject.getFullYear(),currentDateObject.getMonth()+1,0);
            let nowMonth = monthList[lastMonthDateObject.getMonth()];
            let thisYear = lastMonthDateObject.getFullYear();
            let response;
            let onlyTimeSheetData=[];
            let timesheetRecord;

            try {
               //First check here request month is same as the current method
              if(monthFromClient===nowMonth && req.session.email!=null){
           //async waterfall starts here 
                waterfall([
                    function getTimesheet(callback){
                      console.log('waterfall execution starts here');

                      try {
                        timesheetRecord = TIMESHEET_DETAILS.findOne({email:req.session.email});
                        console.log(timesheetRecord.email);
                        // onlyTimeSheetData=timesheetRecord.timesheetData[0];
                      } catch (e) {
                        console.log(e);
                        callback(e,null);
                        return;
                      }

                      console.log(onlyTimeSheetData);
                      callback(null,onlyTimeSheetData);
                    },

                    function insertNewDataInFormat(err,onlyTimeSheetData) {
                      if(err){
                        console.error(err);
                      }else {
                        console.log(onlyTimeSheetData);
                      }

                    }
                ]);
                response='successful';
              }else {
                response='not successful';
              }
            } catch (e) {
              console.error('error find in main catch'+e);
            } finally {
              res.send(response);
            }

findOne方法是一個異步調用,您在沒有回調的情況下調用它。 嘗試使用回調或promise調用findOne

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM