简体   繁体   中英

How to fix Buffered timeout error in nodejs & mongoose

I have written the following API:

const express = require("express");
const router = express.Router();
const {getAttendanceSheet,getDailyAttendance} = require("../../services/attendanceService");


router.get("/daily/:date/:location/:workerType", async (req, res) => {
  const {date,location,workerType} = req.params;
  try {
    const serviceResponse = await getDailyAttendance(date,location,workerType)
    res.status(200).json(serviceResponse);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
});

Which uses getDailyAttendance service function from the service file, as you can see

const formatTimeToIso = require("../helpers/timeFormat/momentISO")
const Attendance = require("../models/Attendance")
const Mongoose = require("mongoose");


exports.getDailyAttendance = async(date,locationId,workerType) => {
        const dailyAttendance = await Attendance.find({
          Date:  formatTimeToIso(date),
          locationId: Mongoose.Types.ObjectId(locationId),
          workerType: workerType,
        }).populate("detections");

        console.log(dailyAttendance);

        return dailyAttendance
}

Now this whole pipeline works fine when I am testing in postman or in the browser

http://localhost:5002/api/attendance/daily/08-01-21/60dd6d303da6c17209d5ef68/Employee

But the problem arises when I am using getDailyAttendance in a separate file other than the route file

const {getDailyAttendance} = require("./src/services/attendanceService")

async function getJSON () {

    try {
        const serviceResponse = await getDailyAttendance("08-01-21","60dd6d303da6c17209d5ef68","Employee")
        console.log(serviceResponse);

      } catch (error) {
        console.log(error);
        
      }
}

getJSON()

Whenever I am running this it's throwing the following error:

MongooseError: Operation `attendances.find()` buffering timed out after 10000ms
    at Timeout.<anonymous> (/home/sp/Desktop/nodejs/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:185:20)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)

I've cross-checked by DB connection by running API in postman which confirms its running status, What's seems to be a problem here?

I resolved this issue by re-establishing the mongoose connection in separate script, there was nothing wrong with syntax

const connectDb = require('./src/config/db')

connectDb()

const {getDailyAttendance} = require("./src/services/attendanceService")

async function getJSON () {

    try {
        const serviceResponse = await getDailyAttendance("08-01-21","60dd6d303da6c17209d5ef68","Employee")
        console.log(serviceResponse);

      } catch (error) {
        console.log(error);
        
      }
}

getJSON()

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