简体   繁体   中英

Mongoose object id is null

I'm creating an object Registration (a Mongoose Schema) and I need to set its Id in User with this line: registrationId: registration._id});

However, the Id is still null , even though it's the callback function? When I check the database the Registration has and Id of course, but not in the callback. How can I set the Id of the Registration in User ?

Edit2: changed to minimal example. This prints out two times 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);
  });
};

Edit: this is the schema (I left out some fields that are not required):

'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);

Problem and solution: the problem was the client sending an attribute _id = null, and that's why MongoDB/Mongoose didn't update the id.

There must be something else going on in your code which is effecting this. This example works as expected for me:

'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);
});

I get a valid id written out to the console.

removing _id from req.body fixed my issue.

if(req.body._id === null) {
  delete req.body._id;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM