【发布时间】:2015-02-13 21:19:38
【问题描述】:
我正在创建一个对象注册(Mongoose 架构),我需要在 User 中设置它的 Id 与此行:registrationId: registration._id});
但是,Id 仍然是null,即使它是回调函数?当我检查数据库时,注册当然有和 Id,但不在回调中。如何在User 中设置Registration 的Id?
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