猫鼬不更新默认状态



hi我有一个具有默认状态的post模式,我只想在它创建新post时更新默认状态,但我的代码没有为此更新它不会用相同的更新和保存新的帖子

这是我的后模式

const postSchema = mongoose.Schema({
title: String,
message: String,
name: String,
tags: [String],
picture: String,
likes: {
type: [String],
default: [],
},
createdAt: {
type: Date,
default: Date.now(), //problem here 
},
profilePicture: String,
userId: String,
comments: {
type: [
{
commentUserId: String,
commentUserName: String,
comment: String,
createdAt: Date,
},
],
},
});
export const createPost = async (req, res) => {
const post = req.body;
const newPost = new Post({ ...post, createAt: new Date() }); // I update it but doesn't work 
try {
await newPost.save();
const user = await User.findById(post.userId);
user.userPosts.unshift(newPost);
await user.save();
res.status(201).json(newPost);
} catch (error) {
res.status(404).json({ message: error.message });
}
};

在发布到堆栈溢出之前,请检查是否有拼写错误。在您的模式中,您已将密钥命名为createdAt。在新的猫鼬对象中,您已将其命名为createAt。(注意丢失的"d"(

最新更新