0

I have a server in nodejs with this schema in json:

    var UserSchema = new Schema({

        nick: String,
        deviceId: String,
        visivel: Boolean,
        checks: [{date: {type:String},log: {type:Number},lng: {type:Number}}]
});

In the Post code I have something , like this:

.post(function(req, res){

            var user = new User();
            user.deviceId = req.body.deviceId;
            console.log("ID: " + req.body.deviceId);

            User.findOne({'deviceId': user.deviceId},'deviceId nick visivel checks',function(err, useritem){

                user.nick = req.body.nick;
                user.visivel = req.body.visivel;

                if(req.body.checks != undefined){
                    req.body.checks.forEach(function(items){
                    console.log(items);
                        user.checks.push(items);
                    });
                }
                user.save(function(err){
                    if (err)
                        res.send(err);

                    res.json({ message: 'user save!' });
                    console.log('user save 1!');

                })
            }
    }

when i make a post with a only one object in check array, the server have a exception:

Object # has no method 'forEach' at Promise. (/RalyAPP/server.js:268:22) at Promise. (/RalyAPP/node_modules/mongoose/node_modules/mpromise
/lib/promise.js:162:8) at Promise.emit (events.js:95:17) at Promise.emit (/RalyAPP/node_modules/mongoose/node_modules/mpromise/lib/pr
omise.js:79:38) at Promise.fulfill (/RalyAPP/node_modules/mongoose/node_modules/mpromise/lib
/promise.js:92:20) at /RalyAPP/node_modules/mongoose/lib/query.js:1833:13 at model.Document.init (/RalyAPP/node_modules/mongoose/lib/document.js:251:1
1) at completeOne (/RalyAPP/node_modules/mongoose/lib/query.js:1831:10) at /RalyAPP/node_modules/mongoose/lib/query.js:1799:11 at /RalyAPP/node_modules/mongoose/lib/utils.js:414:16

But if i make a post with more that one, run well and not have a exception.

Can help resolve this problem?

bests.

Góis
  • 27
  • 1
  • 1
  • 10
  • Your `req.body.checks` is not an array. Try to check if it's an array or not at first and then do your logic – Vsevolod Goloviznin Jan 03 '15 at 21:50
  • just food for thought, you have `if (req.body.checks != undefined)`, you should always use `!==` when checking a value against `undefined` because javascript will try type conversions before evaluating the value... not an answer but thought i should put in my two cents, http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons – undefined Jan 03 '15 at 23:26

2 Answers2

1

Something like this perhaps?

if (req.body.checks !== undefined) {
    if (Array.isArray(req.body.checks)) {
        req.body.checks.forEach(function(items) {
            console.log(items);
            user.checks.push(items);
        });
    } else {
        console.log(req.body.checks);
        user.checks.push(req.body.checks);
    }
}
CLECode
  • 158
  • 8
  • What is the point of `if (req.body.checks !== undefined) {` when immediately followed by `if (Array.isArray(req.body.checks))`? Seems to me the first is redundant. ;-) – RobG Jan 03 '15 at 23:59
  • Because if you are only checking if it is array or not, you will not know if it's undefined and therefore may be pushing undefined into user.checks.push.... – CLECode Jan 04 '15 at 09:31
0

Request that comes to you contain the body in string (or) json format. You need to deserialize it and convert it into an array object. (or) you can simply use express#bodyParser. if you are sending data with type as application/json, add this in server.js,

var bodyParser = require('body-parser');
app.use( bodyParser.json() );