【问题标题】:Mongoose object id is nullMongoose 对象 ID 为空
【发布时间】:2015-02-13 21:19:38
【问题描述】:

我正在创建一个对象注册(Mongoose 架构),我需要在 User 中设置它的 Id 与此行:registrationId: registration._id});

但是,Id 仍然是null,即使它是回调函数?当我检查数据库时,注册当然有和 Id,但不在回调中。如何在User 中设置RegistrationId

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

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

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

问题与解决方案:问题是客户端发送属性_id = null,这就是MongoDB/Mongoose没有更新id的原因。

【问题讨论】:

  • 好的,我更改了第一个代码部分。

标签: node.js mongodb express mongoose


【解决方案1】:

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

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

【讨论】:

  • 另外,您也可以使用 undefined 代替 null ,它会为您生成一个 id。在我的情况下,将这一行上的 null 更改为 undefined 修复了它:_id: newUser ? undefined : selectedUser._id
【解决方案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);
});

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

【讨论】:

  • 不幸的是,这并没有解决问题。 registration._id 保持为空。真不知道为什么在生成id之前回调就执行了。
  • 让我们看看您的注册架构定义。
  • 一定有其他事情发生。请参阅适用于我的更新示例。
  • 你说数据库里面的值确实有一个有效的_id?我想知道 _id 是否在 req.body 中定义,导致它为空。
  • 非常感谢。我在req.body 中将_id 设为null 时遇到了同样的问题。
猜你喜欢
  • 2019-01-11
  • 2018-03-26
  • 2015-05-25
  • 2016-07-28
  • 2018-11-13
  • 1970-01-01
  • 2018-08-07
  • 2019-12-17
  • 2020-07-26
相关资源
最近更新 更多