0

My Code:

var mySchema = new Schema({
  aktuell: {N7:String, Auftragsnr:String},
  historie : [new Schema({
                date: { type: Date, default: Date.now },
                changed: {N7:String, Auftragsnr:String},
                deleted: { type: Boolean, default: false }
             }, {_id:false})]
}, { strict: false });

createIP = function(req, res) {
    var ip = new IP ({
                      aktuell:     req.body.aktuell,
                      historie:    [
                          {
                            date: nowObj, 
                            changed: req.body.aktuell
                          }
                      ]
    });

    ip.save(function(err) {
        if(err) {
            res.send({status:err});
            return;
      } else {
            res.send({ status: 'OK', ip:ip });
      }
    });
}

I expect it to create such entry in my DB:

{ _id: 54181fc0c5b9c1c7294ef510,
    historie: 
    [ { deleted: false,
        changed: [Object],
        date: Tue Sep 16 2014 13:32:16 GMT+0200 (CEST) 
    } ],
    aktuell: { Auftragsnr: '12', N7: '123132' } }

But in addition to above object literal an additional historie object gets created with its own ObjectId. This is why I used {_id:false} in my model. I also declared my array as new Schema as advised by the Mongoose Docs. I am out of knowlodge, I tried so much. How to get rid of this bug?

A console.log(ip) shows that indeed two object literals are passed by mongoose to mongoDD, one with an empty aktuell object, and one with a filled one. Also the changed object behaves strangely. What is going on here?

Im listening on port 9000
Connected to Database
POST - /ip/create
{ __v: 0,
  _id: 54182c3bd9d529df6085f853,
  historie: 
   [ { deleted: false,
       changed: {},
       date: Tue Sep 16 2014 14:25:31 GMT+0200 (CEST) } ],
  aktuell: {} }
IP created
POST /ip/create/ 200 25.763 ms - 128
POST - /ip/create
{ __v: 0,
  _id: 54182c3bd9d529df6085f854,
  historie: 
   [ { deleted: false,
       changed: [Object],
       date: Tue Sep 16 2014 14:25:31 GMT+0200 (CEST) } ],
  aktuell: { Auftragsnr: '14', N7: 'qweqw' } }
IP created
POST /ip/create/ 200 23.870 ms - 214

1 Answer 1

1

If you don't want an id on your nested documents and don't want to access them outside your ip object you don't need to create another schema for it. A new schema usually means a new collection and a reference to it. Instead, just have it be nested:

var IpSchema = new Schema({
    aktuell: {
        N7: { type: String },
        Auftragsnr: { type: String },
    },
    historie: [
        {
            _id: false,
            date: { type: Date, default: Date.now },
            changed: {
                N7: { type: String },
                Auftragsnr: { type: String },
            },
            deleted: { type: Boolean, default: false },
        }
    ],
});

Think this will also solve your problem, let me know.

Sign up to request clarification or add additional context in comments.

2 Comments

Your data structure is nice. Still , fyi, the other worked fine as well! The root-cause of the problem: Turns out it was due to a preflight header request "OPTIONS" from angular before the actual "POST" request had been made. My quest continues in findind undocumented features and pitfalls of angular and mongoose.
Ok I see. Hope the other issue will gets solved then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.