[英]How to fix Buffered timeout error in nodejs & mongoose
我写了以下 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 });
}
});
如您所见,它使用服务文件中的getDailyAttendance服务 function
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
}
现在,当我在 postman 或浏览器中测试时,整个管道工作正常
http://localhost:5002/api/attendance/daily/08-01-21/60dd6d303da6c17209d5ef68/Employee
但是当我在路由文件以外的单独文件中使用getDailyAttendance时,问题就出现了
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()
每当我运行它时,它都会抛出以下错误:
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)
我通过在 postman 中运行 API 来确认其运行状态,通过数据库连接进行了交叉检查,这里似乎有什么问题?
我通过在单独的脚本中重新建立 mongoose 连接解决了这个问题,语法没有问题
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.