[英]I am using async-waterfall and facing some issues
I simply want to fetch the details from a document called 'slt_timesheet'. 我只是想从名为“ slt_timesheet”的文档中获取详细信息。 And I'm using async-waterfall to run 2-3 function in a sequence. 我正在使用async-waterfall在序列中运行2-3函数。
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. 如果我使用“ 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
method is an asynchronous call and you are calling it without callback. findOne
方法是一个异步调用,您在没有回调的情况下调用它。 Try calling findOne
with either callback or promises. 尝试使用回调或promise调用findOne
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.