我正在使用express和mongodb在服务器上创建应用程序。我以前在开发此应用程序方面做得很好,直到做了一个小的更改,我的所有查询都无法在mongodb上工作。我对连接字符串进行了三重检查,邮递员的输出已到达该函数,但从未触发mongodb查询回调。
这是我的server.js
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const authenticationController = require("./controllers/authenticationController");
const mainActionsController = require("./controllers/mainActionsController");
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cors());
// all user actions
app.use("/auth", authenticationController);
app.use("/memoryleak", mainActionsController);
// Fail safe. Here's our classic 404 error :P
app.all("*", (req, res) => {
res
.status(404)
.send(
"Sorry, the content you were looking for was not found. The classic 404 "
);
});
const port = process.env.PORT || 3001;
app.listen(port, function() {
console.log("Express server listening on port " + port);
});
module.exports = { app };
这是我的mainActionsController的一个片段,该片段在上方触发并使用了路由器...
const router = express.Router();
const mongoose = require("mongoose");
const { Questions, Replies, Users } = require("../dbschema");
const repliesController = require("./repliesController");
const questionController = require("./questionController");
const notificationController = require("./notificationsController");
router.get("/getQuestionById/:id", (req, res) => {
questionController.getQuestionById(req, res, Questions, mongoose);
});
router.get("/questions", (req, res) => {
questionController.getAllQuestions(req, res, Questions);
});
module.exports = router;
这是我的getAllQuestions函数:
const getAllQuestions = (req, res, Questions) => {
console.log("got to the correct function");
Questions.find({}, (err, docs) => {
console.log("hello? CAN YOU HEAR ME");
console.log(err, docs);
if (err) {
console.log(err);
return res.status(400).send(err);
}
console.log(docs);
return res.status(200).send(docs);
});
};
当邮递员打到路由时,将到达所有这些控制台日志,但是回调函数内部没有任何记录,并且连接只是挂起。
最后,这是我的数据库架构
const mongoose = require("mongoose");
const UserSchema = new mongoose.Schema({
userName: { type: String, unique: true, required: true },
passWord: { type: String, required: true },
questionsAsked: { type: Array, required: true },
repliesGiven: { type: Array, required: true },
itemsLiked: { type: Array, required: true },
notifications: { type: Array, required: true }
});
const QuestionSchema = new mongoose.Schema({
title: { type: String, required: true },
questionBody: { type: String, required: true },
numReplies: { type: Number, required: true },
numLikes: { type: Number, required: true },
numVotes: { type: Number, required: true },
tags: { type: Array, required: true },
contributor: { type: String, required: true }
});
const ObjectId = mongoose.Schema.Types.ObjectId;
const ReplySchema = new mongoose.Schema({
questionID: ObjectId,
title: String,
questionBody: String,
numLikes: Number,
numVotes: Number,
contributor: String
});
mongoose.model("Reply", ReplySchema);
mongoose.model("Question", QuestionSchema);
mongoose.model("User", UserSchema);
module.exports = {
Questions: mongoose.model("Question"),
Replies: mongoose.model("Reply"),
Users: mongoose.model("User")
};[1]
任何反馈将不胜感激。