Node.js 在mongoose中尝试保存时出错?

Node.js 在mongoose中尝试保存时出错?,node.js,mongoose,body-parser,Node.js,Mongoose,Body Parser,我试图在discussionReplyingSchema中保存一些数据,然后将其连接到讨论模式。这是我的两个讨论和答复示意图: const mongoose = require('mongoose'); const ObjectID = mongoose.Schema.Types.ObjectId; let discussionSchema = mongoose.Schema({ title: {type: String, required: true}, content: {

我试图在discussionReplyingSchema中保存一些数据,然后将其连接到讨论模式。这是我的两个讨论和答复示意图:

const mongoose = require('mongoose');
const ObjectID = mongoose.Schema.Types.ObjectId;

let discussionSchema = mongoose.Schema({
    title: {type: String, required: true},
    content: {type: String},
    author: {type: ObjectID, required: true, ref: 'User'},
    date: {type: Date, default: Date.now()},
    reply: [{ type: ObjectID, ref: 'Replying' }]
});

let discussionReplyingSchema = mongoose.Schema({
    content: {type: String, required: true},
    author: {type: mongoose.Schema.Types.ObjectId, required: true, ref: 'User'},
    date: {type: Date, default: Date.now()}
});

const Discussion = mongoose.model('Discussion', discussionSchema);
const Replying = mongoose.model('Replying', discussionReplyingSchema);

module.exports = Discussion;
module.exports = Replying;
在控制器中,我获得了用户和内容,并试图将它们保存在mongoose中,但我得到了错误。代码:

replyPost: (req, res) => {
        let replyParts = req.body;

        let replyContent = req.body.replyContent;
        console.log(replyContent);

        let userId = req.user.id;
        replyParts.author = userId;

        Replying.create(replyParts).then(reply => {
            req.user.reply.push(reply.id);
            req.user.save(err => {
               if (err) {
                res.render('/');
               } else{
                res.redirect('/');
               }
            });
        });
    }
数据库现在就是这样的:

“答复”:[…]


我这样做是为了我的项目,如果可能的话,是否有人能向我解释错误在哪里,并在可能的情况下进行修复?

看起来您可能希望使用replyContent vs replyParts进行保存,但我不确定。您的console.log(replyContent)打印了什么?另外,在你的承诺中添加一个陷阱,这样你就可以捕获错误并查看发生了什么,这很可能只是一个模式差异

replyPost: (req, res) => {
    let replyParts = req.body;
    console.log('replyParts', replyParts);

    let replyContent = req.body.replyContent;
    console.log('replyContent', replyContent);

    let userId = req.user.id;
    replyParts.author = userId;

    Replying.create(replyParts).then(reply => {
        req.user.reply.push(reply.id);
        req.user.save(err => {
           if (err) {
             console.log('USER SAVE ERR:' + err);
            res.render('/');
           } else{
            res.redirect('/');
           }
        });
    }).catch(err => console.log('REPLYING ERR:' + err));
}

它可能在这里爆炸
let userId=req.user.id
您可能没有一个
用户
绑定到您的
请求
,因此id类型错误,因此代码在运行时甚至在您开始答复
之前就爆炸了。create(),只需将用户传递到函数,类似于您已经在使用
req.body.replyContent
时所做的操作。(只需将用户从前端或父函数连接到请求正文)将此添加到函数顶部:
console.log('doi have a user?',req.user)
如果您确实有用户,可能是您的
回复
没有
id
,可能是
\u id
?回复后的
回复
是什么样子。创建(replyParts)。然后(reply=>{
还有
replyParts
应该有一个名为
content
的键,而不是
replyContent
没错,有一个名为“content”的键,但它不是“replyContent”吗做同样的事情吗?我不能控制台日志回复,可能在它崩溃之前?我可以从github发送一个链接来查看整个代码,如果这对您来说不是问题的话?