由于某些原因,我的API无效。正在发送错误的响应,因此我可以将问题缩小到以下代码。
const express = require('express');
const router = express.Router();
const MongoClient = require('mongodb').MongoClient;
const ObjectID = require('mongodb').ObjectID;
const connection = (closure) => {
return MongoClient.connect('mongodb://localhost:27017/manatee', (err, db) => {
if (err) return console.log(err);
closure(db);
});
};
const sendError = (err, res) => {
response.status = 501;
response.message = typeof err == 'object' ? err.message : err;
res.status(501).json(response);
};
let response = {
status: 200,
data: [],
message: null
};
router.get('/tasks', (req, res) => {
connection((db) => {
db.collection('tasks').find()
.toArray()
.then((tasks) => {
response.data = tasks;
res.json(response);
})
.catch((err) => {
sendError(err, res);
});
});
});
module.exports = router;
我正在使用mongodb ^ 2.2.34。谁能在这里发现问题?我很遗憾它可能是什么,任何帮助将不胜感激