【发布时间】:2021-08-13 19:16:09
【问题描述】:
难倒这里,应该是非常简单的事情。
我有一个 MERN 堆栈应用程序,它没有按预期从 mongo 中找到数据。
我从前端发布并更新文档。我登录到 Mongo CLI,我可以看到我保存的数据。
但是对于 Node 应用程序,Mongoose 不会返回完整的文档。
这是我获取文档的途径——我什至尝试测试获取 Everything。
router.get("/", async (req, res) => {
const user_email = req.query.user_email;
const course_id = req.query.course_id;
const test = await CourseProgressSchema.find({
_id: "60acfd1c969cac0bd3a213a8",
});
console.log(test);
try {
let course_progress;
if (user_email && course_id) {
course_progress = await CourseProgressSchema.findOne({
user_email,
course_id,
});
if (!course_progress) {
const newCourseProgress = new CourseProgressSchema({
user_email,
course_id,
});
course_progress = await newCourseProgress.save();
}
} else if (user_email && !course_id) {
course_progress = await CourseProgressSchema.find({ user_email });
} else if (course_id && !user_email)
course_progress = await CourseProgressSchema.find({ course_id });
else {
res.json({ error: "Not Found." });
}
console.log(course_progress);
res.json({ success: course_progress });
} catch (error) {
console.log(error);
res.json({
error: "Soemthing went wrong when getting current course progress.",
});
}
});
course_progress 被安慰/返回为:
[0] [
[0] {
[0] _id: 60acfd1c969cac0bd3a213a8,
[0] user_email: 'nickisyourfan@icloud.com',
[0] course_id: '60acfcfe969cac0bd3a213a7',
[0] __v: 0
[0] }
[0] ]
但如果我访问 mongo cli 并使用 db.courseprogressscchemas.find().pretty() 它会返回更新后的文档:
{
"_id" : ObjectId("60acfd1c969cac0bd3a213a8"),
"user_email" : "nickisyourfan@icloud.com",
"course_id" : "60acfcfe969cac0bd3a213a7",
"__v" : 0,
"course_progress" : {
"60acfca1969cac0bd3a213a5" : {
}
}
}
这是我的架构 - 没什么特别的:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const CourseProgressSchema = new Schema({
user_email: {
type: String,
required: true,
},
course_progress: {
type: Object,
required: true,
default: {},
},
course_id: {
type: String,
required: true,
},
});
module.exports = CourseProgress = mongoose.model(
"CourseProgressSchema",
CourseProgressSchema
);
谁能帮我弄清楚为什么猫鼬只返回文件的一部分而不是整个文件?
【问题讨论】: