简体   繁体   中英

I am using async-waterfall and facing some issues

I simply want to fetch the details from a document called 'slt_timesheet'. And I'm using async-waterfall to run 2-3 function in a sequence.

If I use the 'get' method to get the details of slt_timesheet then I got all the details regarding my slt-timesheet but if I use async-waterfall I am unable to get the details from document.

    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 method is an asynchronous call and you are calling it without callback. Try calling findOne with either callback or promises.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM