【发布时间】:2021-07-07 08:17:21
【问题描述】:
我正在尝试将由 MongoDB 模型创建的对象作为发布请求的主体发送到一个开玩笑的测试中。 这是测试:
test("post request successfully creates a new blog", async () => {
const newBlog = new Blog({
title: "My mundane day at home 3",
author: "AbG",
url: "https://www.google.co.in/",
likes: 11,
});
await api
.post("/api/blogs")
.send(newBlog)
.expect(201)
.expect("Content-Type", /application\/json/)
.catch((err) => console.log(err));
const blogs = await Blog.find({});
expect(blogs).toHaveLength(initialBlogs.length + 1);
});
如您所见,我将 newBlog 作为请求正文发送。但是当我在控制器中收到它时,newBlog 出现在 request.body._doc 而不是 request.body 中。 我认为这与博客的猫鼬模型有关。
const blogSchema = new mongoose.Schema({
title: {
type: String,
required: true,
},
author: {
type: String,
required: true,
},
url: {
type: String,
required: true,
},
likes: {
type: Number,
required: false,
default: 0,
},
});
module.exports = mongoose.model("Blog", blogSchema);
我不明白为什么会这样。
【问题讨论】:
-
本次讨论是否回应了您的问题? stackoverflow.com/questions/48989100/…
-
ĐăngKhoaĐinh 这个问题与我的问题类似。尽管解决方案不一样,但我设法找到了解决方案。感谢您的帮助!
标签: node.js express mongoose jestjs