I have a problem when trying to save my model after pushing data to it: Here are Schemas:
var ClientSchema = new Schema({
email: {type: String, required: '{PATH} is required!', unique: 'This email already exists!'},
name: String,
nick: String,
domains: [{ type: Schema.Types.ObjectId, ref: 'Domain' }],
hosting: [{ type: Schema.Types.ObjectId, ref: 'Hosting'}]
});
var DomainSchema = new Schema({
name: {type: String, required: '{PATH} is required'},
startDate: {type: Date, required: '{PATH} is required'},
endDate: {type: Date, required: '{PATH} is required'},
realPrice: {type: Number, required: '{PATH} is required'},
billedPrice: {type: Number, required: '{PATH} is required'}
});
and here's a part of the controller:
...
Client.findOne({_id: req.params.client_id},function(err,client) {
var domain = new Domain({
name: req.body.name,
startDate: req.body.startDate,
endDate: req.body.endDate,
realPrice: req.body.realPrice,
billedPrice: req.body.billedPrice
});
domain.save(function(err) {
console.log(domain._id);
});
cli.domains.push(domain._id);
client.save(function(err,cli) {
// That's the one that makes it possible to save
// client.save();
});
res.redirect("/");
});
...
Now,
if I leave it as it is with one client.save(..)
, the domain is saved, it's pushed but the client is not saved.
If I uncomment another client.save()
everything saves as it should (I guess).
So the question again, why do I need to save twice?
Am I missing sth really simple here?
Don't get me wrong - it works, but I just need to understand it ;)
Thanks for your help in advance.
UPDATE/SOLUTION:
All the problems I had were because of the older (2.4.9) version of mongodb installed on my ubuntu machine and there were some conflicts with the versioning of documents that mongodb uses. I checked the same code on another machine and everything worked as supposed. That's what made me recheck and install newer mongodb together with cleaning the actual db which made everything just as it should be and of course - working ;)