猫鼬对象ID为null

Aar*_*ron 5 mongoose mongodb node.js express

我正在创建一个对象注册(猫鼬模式),我需要IdUser此行中设置它:registrationId: registration._id});

但是,即使是回调函数,它Id仍然是null?当我检查数据库时,“注册”当然有ID,但是没有在回调中。我如何设置IdRegistrationUser

Edit2:更改为最小示例。打印两次null

exports.create = function(req, res) {
  Registration.create(req.body, function(err, registration) {
    if(err) { return handleError(res, err); }

    console.log(registration._id);
    console.log(registration.id);

    return res.json(201, registration);
  });
};
Run Code Online (Sandbox Code Playgroud)

编辑:这是架构(我省略了一些不需要的字段):

'use strict';

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

var RegistrationSchema = new Schema({

    isReservation: Boolean,

    //Id of the trip
    tripId: String,

    //socialMutuality
    codeGerechtige: String,
    socialMutualityNumberParent1: String,
    socialMutualityNumberParent2: String,

    //contact
    userId: String,
    firstnameContact: String,
    lastnameContact: String,
    emailContact: String,
    streetContact: String,
    streetNumberContact: String,
    zipcodeContact: String,
    busContact: String,
    cityContact: String,
    phoneContact: String,
    gsmContact: String,
    socialSecurityNumberContact: String,

    //coordinats of person that pays

    //child information

    //emergency contacts
    emergencyContacts: [{
        firstName: String,
        lastName: String,
        phone: String
    }],

    extraInfo: String

});

module.exports = mongoose.model('Registration', RegistrationSchema);
Run Code Online (Sandbox Code Playgroud)

问题与解决方案:问题在于客户端发送了_id = null属性,这就是为什么MongoDB / Mongoose不更新ID的原因。

小智 6

从 req.body 中删除 _id 解决了我的问题。

if(req.body._id === null) {
  delete req.body._id;
}
Run Code Online (Sandbox Code Playgroud)


Tim*_*ple 2

您的代码中一定还有其他事情正在影响这一点。这个例子对我来说按预期工作:

'use strict';

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

mongoose.connect('mongodb://localhost/createid');

var RegistrationSchema = new Schema({

    isReservation: Boolean,

    //Id of the trip
    tripId: String,

    //socialMutuality
    codeGerechtige: String,
    socialMutualityNumberParent1: String,
    socialMutualityNumberParent2: String,

    //contact
    userId: String,
    firstnameContact: String,
    lastnameContact: String,
    emailContact: String,
    streetContact: String,
    streetNumberContact: String,
    zipcodeContact: String,
    busContact: String,
    cityContact: String,
    phoneContact: String,
    gsmContact: String,
    socialSecurityNumberContact: String,

    //coordinats of person that pays

    //child information

    //emergency contacts
    emergencyContacts: [{
        firstName: String,
        lastName: String,
        phone: String
    }],

    extraInfo: String

});

var Registration = mongoose.model('Registration', RegistrationSchema);

var reg = {
    userId: '1234',
    tripId: '2345',
    firstnameContact: 'Timothy',
    lastnameContact: 'Strimple',
    emailContact: 'tim@tstrimple.com'
};

Registration.create(reg, function(err, registration) {
    if(err) { throw err; }
    console.log(registration._id);
});
Run Code Online (Sandbox Code Playgroud)

我得到一个写到控制台的有效 ID。

  • 你说数据库里面的值确实有一个有效的_id?我想知道 _id 是否在 req.body 中定义,导致它为空。 (5认同)
  • 肯定还有其他事情发生。请参阅我更新后的示例,该示例对我有用。 (2认同)