1

I'm trying to do:

$.ajax({
    type:"POST",
    url:"/psychos",
    data:JSON.stringify(this.psycho)        
})

At server I got:

app.post("/psychos", function(request, response) {

    var psychologist = request.body.psycho
    console.log(psychologist)


    psicologosCollection.insert(psychologist, function(error, responseFromDB) {

        if (error) {response.send(responseFromDB)}
        
        console.log("Se ha insertado: "+ JSON.strinfigy(responseFromDB))
        response.send(responseFromDB)
    })
})

But, the console.log() is printing undefined and got the following throw:

TypeError: Cannot read property '_id' of undefined
    at insertWithWriteCommands (/home/diegoaguilar/DigitalOcean/psicologostuxtepec/node_modules/mongodb/lib/mongodb/collection/core.js:78:13)
    at Collection.insert (/home/diegoaguilar/DigitalOcean/psicologostuxtepec/node_modules/mongodb/lib/mongodb/collection/core.js:30:7)
    at Object.handle (/home/diegoaguilar/DigitalOcean/psicologostuxtepec/server.js:117:25)
    at next_layer (/home/diegoaguilar/DigitalOcean/psicologostuxtepec/node_modules/express/lib/router/route.js:103:13)
    at Route.dispatch (/home/diegoaguilar/DigitalOcean/psicologostuxtepec/node_modules/express/lib/router/route.js:107:5)
    at c (/home/diegoaguilar/DigitalOcean/psicologostuxtepec/node_modules/express/lib/router/index.js:195:24)
    at Function.proto.process_params (/home/diegoaguilar/DigitalOcean/psicologostuxtepec/node_modules/express/lib/router/index.js:251:12)
    at next (/home/diegoaguilar/DigitalOcean/psicologostuxtepec/node_modules/express/lib/router/index.js:189:19)
    at next (/home/diegoaguilar/DigitalOcean/psicologostuxtepec/node_modules/express/lib/router/index.js:166:38)
    at Layer.session [as handle] (/home/diegoaguilar/DigitalOcean/psicologostuxtepec/node_modules/express-session/index.js:98:29)

I'm using BodyParser before:

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

psycho is actually a valid existing object. At the method where AJAX is performed a console.log(this.psycho) will print my object. What am I doing wrong?

Edit:

Even I found out I should have got:

var psychologist = request.body.psycho

at server GET route code, I can't understand how bodyParser generates an Object?

For a quite similar AJAX call I tried before:

function getSendingJSON(url,reqObject,callBack)  {
    $.ajax({
        type: "get",
        data: reqObject,
        dataType: "json",
        url: url,
        success: function(response){
           callBack(response);
        }
    });
}

So response was a JSON, and callback function looked similar to:

function plotReglaFalsa(respuesta) {

    if (respuesta.fail) {...}
    else if (respuesta.negative) {...}
    ....
}

Even that was purely at client side, I'm confused how to deal with Objects/JSON serialization and how bodyParser acts for it.

Community
  • 1
  • 1
diegoaguilar
  • 8,179
  • 14
  • 80
  • 129
  • So what comes out of (and even better) `console.log( JSON.stringify(psycologist) )` then? And you should also be doing that in your `$ajax` request as well to serialize `data: JSON.stringify( this.psycho )` – Neil Lunn May 21 '14 at 05:40
  • I'm getting undefined too. – diegoaguilar May 21 '14 at 05:45
  • possible duplicate of [Serializing to JSON in jQuery](http://stackoverflow.com/questions/191881/serializing-to-json-in-jquery) – mpm May 21 '14 at 05:46

2 Answers2

3

You have not serialized the data in your request, it's still just a JavaScript object until you serialize it as JSON:

$.ajax({
    type:"POST",
    contentType: "application/json",
    url:"/psychos",
    data: JSON.stringify( this.pyscho )
})

So call JSON.stringify in order to serialize as JSON and then the body parser has something to parse.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • I tried with `JSON.stringify( this.pyscho )` at client side (inside an Angular controller) and `JSON.parse(request.body.psycho)` at server side but it's still failing – diegoaguilar May 21 '14 at 05:53
  • @diegoaguilar If you are using the body parser then you do not need to parse again as it is already done. – Neil Lunn May 21 '14 at 05:56
  • Also tried with out it, but it fails too: `TypeError: Cannot read property '_id' of undefined ` and a lot of stacktrace – diegoaguilar May 21 '14 at 05:57
  • @diegoaguilar The important thing is seeing the result in `request.body.pyscho` in the first `console.log`. Try logging `request.body` instead as the data there may not be what you think it is. I'm guessing you presumed your posted data would be under the key `psycho` but it probably isn't. – Neil Lunn May 21 '14 at 06:03
  • Exactly, that was the solution. One more question. At `console.log("Se ha insertado: "+ JSON.strinfigy(responseFromDB))` I obtain an error: `TypeError: Object # has no method 'strinfigy'` Why? – diegoaguilar May 21 '14 at 06:05
  • 1
    @diegoaguilar It's spellled incorrectly `stringify`. Anyhow you question is answered. Thanks for the endorsement. – Neil Lunn May 21 '14 at 06:09
1
app.post("/psychos", function(request, response) {
 //change request.body.psycho to request.body
 var psychologist = request.body
 console.log(psychologist)
 psicologosCollection.insert(psychologist, function(error, responseFromDB) {

 if (error) {response.send(responseFromDB)}

    console.log("Se ha insertado: "+ JSON.stringify(responseFromDB))
    response.send(responseFromDB)
})
})
  • Thanks @raju can you extend in your question and explain a bit about it. One more question. At `console.log("Se ha insertado: "+ JSON.strinfigy(responseFromDB))` I obtain an error: `TypeError: Object # has no method 'strinfigy'` Why? – diegoaguilar May 21 '14 at 06:06
  • 1
    @diegoaguilar request.body have the post object if you need the particular key value of that object means you can use req.body.urObjKeyName if you want whole object means you should use req.body..Then change JSON.strinfigy to JSON.stringify... –  May 21 '14 at 06:57