0

I have this "schema" in mongoose, an array in my dictionary and have other embedded array,

I can save if no embedded object is present, how to save embedded objects?

var ReportSchema   = new Schema({
    appVersion: { type: String, required: true},
    osVersion: { type: String, required: true},
    deviceType: { type: String, required: true},
    userID: { type: String, required: true},
    sessionIDtimestamp: { type: String, required: true},

    eventItem : [new Schema ({
        eventType:{type :String},
        startTime:{type :String},
        endTime:{type :String},
        params:[new Schema ({
            val:{type :String}
        })]
    })]
});

on my router:

apiRouter.route('/report') 
    .post(function(req, res) {
        var report = new Report();       

        report.appVersion = req.body.appVersion;   
        report.osVersion = req.body.osVersion;   
        report.deviceType = req.body.deviceType;    
        report.userID = req.body.userID;   
        report.sessionIDtimestamp = req.body.sessionIDtimestamp;   

        for (var i = req.body.events.length - 1; i >= 0; i--) {
            var requestStringa = util.inspect(req.body.events, {showHidden: false, depth: null});
            console.log("entro :"+requestStringa);      
        };

        report.save(function(err) {
            if (err) {
                return res.send(err);
            }
            res.json({ message: 'report created!' });
        });
    })

also i dont think that way to enumerate the array is nice?

edit the log of events

entro :[ { eventType: 'Account_Rated_Pressed',
    startTime: '1435819399',
    endTime: '1435819399',
    params: [ { paramKey: 'rating', paramValue: '5' } ] },
  { eventType: 'RateableDetail',
    startTime: '1435819399',
    endTime: '1435819399',
    params: [ { paramKey: 'rating', paramValue: '5' } ] } ]

how to save my embedded objects cheers

3
  • Can you show us the data for req.body.events? Commented Jul 2, 2015 at 13:10
  • Since that's already an array, can't you just do report.eventItem = req.body.events;? Commented Jul 2, 2015 at 13:20
  • 1
    @chridam correct! thanks! post as answer to get the points, thanks! Commented Jul 2, 2015 at 13:28

1 Answer 1

1

For saving embedded documents, just assign the array field eventItem the request object value as follows:

apiRouter.route('/report') 
    .post(function(req, res) {
        var report = new Report();       

        report.appVersion = req.body.appVersion;   
        report.osVersion = req.body.osVersion;   
        report.deviceType = req.body.deviceType;    
        report.userID = req.body.userID;   
        report.sessionIDtimestamp = req.body.sessionIDtimestamp;   
        report.eventItem = req.body.events;

        report.save(function(err) {
            if (err) {
                return res.send(err);
            }
            res.json({ message: 'report created!' });
        });
    })

In the instance that req.body.event is an object not an array, you would then need to use the JavaScript push() method to push the object to the array. Say for example, if req.body.event has the structure

{ 
    eventType: 'Account_Rated_Pressed',
    startTime: '1435819399',
    endTime: '1435819399',
    params: [ { paramKey: 'rating', paramValue: '5' } ] 
}

you can then do

apiRouter.route('/report') 
    .post(function(req, res) {
        var report = new Report();       

        report.appVersion = req.body.appVersion;   
        report.osVersion = req.body.osVersion;   
        report.deviceType = req.body.deviceType;    
        report.userID = req.body.userID;   
        report.sessionIDtimestamp = req.body.sessionIDtimestamp;   
        report.eventItem.push(req.body.event);

        report.save(function(err) {
            if (err) {
                return res.send(err);
            }
            res.json({ message: 'report created!' });
        });
    })
Sign up to request clarification or add additional context in comments.

2 Comments

thanks! is obvious now! wow nosql and new thinking! thanks a lot ;)
@MaKo No worries! I've also added some useful bits to the answer in the event that the req.body.event is an object not an array.

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.