【发布时间】:2021-10-22 14:57:17
【问题描述】:
我编写了以下 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服务功能,如您所见
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
}
现在,当我在邮递员或浏览器中进行测试时,整个管道工作正常
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 对 DB 连接进行了交叉检查,确认了它的运行状态,这里似乎有什么问题?
【问题讨论】:
标签: javascript node.js express asynchronous mongoose