RangeError:超出最大调用堆栈大小(Mongoose + Express)

时间:2018-01-25 06:57:30

标签: javascript node.js mongodb express mongoose

当我提交我的内容时,它会显示 ERR_EMPTY_RESPONSE 。在我的节点控制台中,它显示错误: RangeError:超出最大调用堆栈大小。当我重新启动我的服务器时,将显示内容字段。在我的数据库中,邮件模型具有内容值,但用户模型没有邮件ID

我使用的是Mongoose 5.0.0。 检查我的数据库时,它显示 messages = []

db.users.find()
 { "_id" : ObjectId("5a771f08ee207b13808a2599"), "firstName" : "rakib", "lastName" : "vai", "password" : "$2a$10$kCGCyFOPCedZcs.4Y6D.Huq02aYycuEssj5suTh00bRJSCGw1N3VO", "email" : "xxxxxx@gmail.com", "messages" : [ ], "__v" : 0 }

在我的短信集合中显示如下

db.messages.find()
{ "_id" : ObjectId("5a771f20ee207b13808a259a"), "content" : "rakibvai", "user" : ObjectId("5a771f08ee207b13808a2599"), "__v" : 0 }

我的消息模型文件:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var User = require('./user');

var schema = new Schema({
 content: {type: String, required: true},
 user: {type: Schema.Types.ObjectId, ref: 'User'}
 });

 schema.post('remove', function (message) {
   User.findById(message.user, function (err, user) {
    user.messages.pull(message);
    user.save();
     });
  });

module.exports = mongoose.model('Message', schema);

我的用户模型文件

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var mongooseUniqueValidator = require('mongoose-unique-validator');

var schema = new Schema({
   firstName: {type: String, required: true},
   lastName: {type: String, required: true},
   password: {type: String, required: true},
   email: {type: String, required: true, unique: true},
   messages: [{type: Schema.Types.ObjectId, ref: 'Message'}]

     });
 schema.plugin(mongooseUniqueValidator);

 module.exports = mongoose.model('User', schema);

my message.js route

router.post('/', function (req, res, next) {
var decoded = jwt.decode(req.query.token);
User.findById(decoded.user._id, function (err, user) {
    if (err) {
        return res.status(500).json({
            title: 'An error occurred',
            error: err
        });
    }
    var message = new Message({
        content: req.body.content,
        user: user
    });
    message.save(function (err, result) {
        if (err) {
            return res.status(500).json({
                title: 'An error occurred',
                error: err
            });
        }
        user.messages.push(result);
        user.save();
        res.status(201).json({
            message: 'Saved message',
            obj: result
        });
    });
  });
});

0 个答案:

没有答案