2

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

thyforhtian
  • 366
  • 3
  • 9

1 Answers1

0

Mongodb is Asynchronous in nature. You need to move client.save() under callback of domain.save. For more help

Client.findOne({_id: req.params.client_id},function(err,client) {

    var domain = new Domain({
        _client: client.id,
        name: req.body.name,
        startDate: req.body.startDate,
        endDate: req.body.endDate,
        realPrice: req.body.realPrice,
        billedPrice: req.body.billedPrice
    });

    domain.save(function(err, result) {
        client.domains.push(result._id);
        client.save();
    });

    res.redirect("/"); 
});

Edit

I have tried your query quickly, its working fine for me.

Check this Sample Code Snippet

Community
  • 1
  • 1
Harpreet Singh
  • 2,651
  • 21
  • 31
  • That's what I tried at first but mongo(ose) is getting me this error: `{ [MongoError: Field name duplication not allowed with modifiers] name: 'MongoError', err: 'Field name duplication not allowed with modifiers', code: 10150, n: 0, connectionId: 333, ok: 1 }` – thyforhtian Jun 22 '14 at 16:43
  • please check my code again - I used cli but It should be client. – Harpreet Singh Jun 22 '14 at 17:26
  • I've noticed it and changed before trying. – thyforhtian Jun 22 '14 at 17:29
  • I tried your query. working fine for me. check the link I have added to the answer – Harpreet Singh Jun 22 '14 at 17:47
  • add console to every step, I am sure you will get the problem. One more thing I have noticed, don't think it is causing much problem but still check it - you have used this "_client: client.id" but your domain schema don't have a "_client" field – Harpreet Singh Jun 22 '14 at 17:54
  • Yeah, everything seems alright yet I still get the error. I'll try to figure it out. Maybe it's sth else causing it. Thanks for your help. – thyforhtian Jun 22 '14 at 17:54
  • Yeah, I removed _client from schema and thanks to console logs get to see this error I'm telling about. – thyforhtian Jun 22 '14 at 17:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56077/discussion-between-harpreet-singh-and-thyforhtian). – Harpreet Singh Jun 23 '14 at 01:11
  • Thanks, but have you cracked it? How? what was the problem? – Harpreet Singh Jun 23 '14 at 16:10
  • :), Ok then please move the reason to comment or Add it in your ques as an Edit it can help other. – Harpreet Singh Jun 23 '14 at 16:14