Всякий раз, когда я пытаюсь сохранить объект пользователя, который:Невозможно сохранить отношения документа с использованием мангуста в node.js
{
"dtUpdate": "2016-06-08T11:32:01.442Z",
"username": "JorgeFerrari",
"password": "kenshin01",
"email": "[email protected]",
"company": null,
"dtCreate": "2016-06-08T11:32:01.442Z",
"_id": "57580231dfd7b61c2184dd64",
"flags": {
"active": true
},
"contacts": [
"57580231dfd7b61c2184dd66",
"57580231dfd7b61c2184dd67"
],
"address": [
"57580231dfd7b61c2184dd65"
],
"info": {
"name": "Jorge Ferrari",
"dateOfBirth": "2016-06-07T00:00:00.000Z",
"gender": "M"
}
}
Я дал следующие ошибки:
{
"message": "User validation failed",
"name": "ValidationError",
"errors": {
"contacts": {
"message": "Cast to Array failed for value \"[object Object],[object Object]\" at path \"contacts\"",
"name": "CastError",
"kind": "Array",
"value": [
{
"type": 51,
"value": "(47) 9685-3200",
"main": true
},
{
"type": 2,
"value": "(47) 3521-8717",
"main": false
}
],
"path": "contacts",
"reason": {
"message": "Cast to ObjectId failed for value \"[object Object]\" at path \"contacts\"",
"name": "CastError",
"kind": "ObjectId",
"value": {
"type": 51,
"value": "(47) 9685-3200",
"main": true
},
"path": "contacts"
}
},
"address": {
"message": "Cast to Array failed for value \"[object Object]\" at path \"address\"",
"name": "CastError",
"kind": "Array",
"value": [
{
"street": "Rua 1601",
"nr": "118",
"neighborhood": "Centro",
"complement": "Apto 702",
"zipcode": "88330807",
"city": "Balneário Camboriú",
"state": "SC",
"main": true
}
],
"path": "address",
"reason": {
"message": "Cast to ObjectId failed for value \"[object Object]\" at path \"address\"",
"name": "CastError",
"kind": "ObjectId",
"value": {
"street": "Rua 1601",
"nr": "118",
"neighborhood": "Centro",
"complement": "Apto 702",
"zipcode": "88330807",
"city": "Balneário Camboriú",
"state": "SC",
"main": true
},
"path": "address"
}
}
}
}
Я действительно не понимаю, что делает эту ошибку, и мне нужна помощь. В моей схеме оба поля адреса и контакта имеют тип: Schema.Types.ObjectId
Как уже было сказано в комментарии, это моя схема. (К сожалению о размере кода)
var userSchema = new Schema({
username: { type: String, required: true, unique: true, index: true },
password: { type: String, required: true , minlength: 128, maxlength: 128 },
email: { type: String, required: true, unique: true, index: true },
profileImage: { type: String },
company: { type: Schema.Types.ObjectId, ref: 'Company' },
info: {
name: { type: String, required: true },
dateOfBirth: { type: Date },
gender: { type: String, enum: ['M', 'F'] }
},
address: [{
type: Schema.Types.ObjectId, ref: 'Address'
}],
contacts: [{
type: Schema.Types.ObjectId, ref: 'Contact'
}],
flags: {
active: { type: Boolean, required: true },
deleted: { type: Boolean },
blocked: { type: Boolean },
newPassword: { type: Boolean }
},
login: {
lastDate: { type: Date },
lastIp: { type: String }
},
dtCreate: { type: Date },
dtUpdate: { type: Date }
});
функции, что делает сохранение:
createUser: function(req, res) {
var _user = new Models.User(req.body);
_user.password = crypto.createHash(_user.password);
for (var index in req.body.address) {
var _address = new Models.Address(req.body.address[index]);
_user.address.push(_address._id);
_address.save(function(err){
});
}
for (var index in req.body.contacts) {
var _contact = new Models.Contact(req.body.contacts[index]);
_user.contacts.push(_contact._id);
_contact.save(function(err) {
});
}
var _dateCreation = new Date();
_user.dtCreate = _dateCreation;
_user.dtUpdate = _dateCreation;
_user.save(function(err) {
if (err) {
res.send(err);
} else {
var _returnUser = Models.User.findById(_user._id, function(err, user) {
if (err) { res.send(err); }
else { res.send(user); }
});
}
});
return;
},
Не могли бы вы показать схему? – jano
@jano отредактирован, чтобы показать схему. –
Посмотрите, может ли помочь вам [этот вопрос] (http://stackoverflow.com/questions/14940660/whats-mongoose-error-cast-to-objectid-failed-for-value-xxx-at-path-id). – Shrabanee