0

I'm struggling to make a function which selects from database based on the id entered in a textbox. I wrote the code, but it shows in console this error and I can't figure why: TypeError: Cannot read property 'id' of undefined.

Code I have so far:

Client-side:

function select()
            {
                var id = $('#nr_reg').val();
                $.ajax({
                    type: 'post',
                    url: '/id',
                    data : {
                        id: id
                    },
                    succes: function(data){
                        var id = data.id;
                        alert(id);
                        $('#optic').val(id);
                    },
                    error: function(err){
                        console.log(err);
                    }

                }); 
            }

Server-side:

app.post('/id', function(req,res) {

    var data = req.body;
    var id = data.id;
    console.log(id);
    var query = "SELECT * FROM Control WHERE id=" +id;
    connection.query(query, function(error, result) {
            console.log(result);
            res.send(result);
    });
});

L.E: Function select:

function select()
            {

                var id = $('#nr_reg').val();
                $.ajax({
                    type: 'post',
                    dataType: 'json',
                    url: '/id',
                    data : {
                        id: id
                    },
                    success: function(data){    
                        data = JSON.parse(data);                    
                        var id = data.id;
                        alert("merge");
                        $('#optic').val(id);
                    },
                    error: function(err){
                        console.log(err);
                    }

                }); 
            }
Ezekiel
  • 303
  • 3
  • 7
  • 25

2 Answers2

2

You said your error is on server side? Add a body parser to your express app:

1st Install body-parser:

npm install body-parser -s

The include this in your app:

var express = require('express');
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.post(/*....*/);

Your app.post must be AFTER app.use line.
Full example here
BTW this is a duplicate of this

Community
  • 1
  • 1
Daniel Gruszczyk
  • 4,945
  • 7
  • 38
  • 74
2

Make sure you've required body-parser. And use bodyParser.urlencoded.

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: false }));

This should be before your .post route definition.

EDIT:

Before all this, you must do npm install body-parser :)

AdityaParab
  • 6,097
  • 3
  • 23
  • 36
  • Ok. This way it works. It gets the id correctly, show the result correctly, but still not entering `succes` in client-side. Why is that anymore? – Ezekiel Sep 22 '15 at 11:39
  • It shows no error. In console shows only the id and the result. But in client-side I have an alert with the id, and it is now showed and still not setting the value of textbox based on what selected. Why? – Ezekiel Sep 22 '15 at 11:41
  • @Ezekiel mmm in your client side code, you have "succes" should be "success" i think no? – Gonzalo Bahamondez Sep 22 '15 at 11:43
  • @Ezekiel : can you paste the value of `data` as received in your `success` callback? – AdityaParab Sep 22 '15 at 11:43
  • You might have to do `data = JSON.parse(data);` first in your success callback. You have not set the `dataType` property in your ajax call. – AdityaParab Sep 22 '15 at 11:45
  • Yes. Done this, and added dataType. But still doing nothing in client-side. – Ezekiel Sep 22 '15 at 11:52
  • First update the spelling of `success`. In your code it is `succes`, like @GonzaloBahamondez pointed out. Then, have you set `dataType:'json'`? And what is getting logged in the console.? – AdityaParab Sep 22 '15 at 11:55
  • I've edited the first post to see exactly how it looks the client-side. It is not doing anything after success and that's all. even if the server-side works fine and shows in the console the right result. – Ezekiel Sep 22 '15 at 11:58
  • @Ezekiel if you are in chrome, use the inspector "Network" tab , to track what happens with the request/response to the "/id" url – Gonzalo Bahamondez Sep 22 '15 at 12:05
  • Not in chrome. Using mozilla in xubuntu. But why it is not entering in that function because the server shows the result correctly, so the query has been successfully .. – Ezekiel Sep 22 '15 at 12:07
  • Ok. The problem is that the data, which I alerted in success is this: `[Object object]` , and data.id says: `undefined` . That's why it is not working. But why is like that? – Ezekiel Sep 22 '15 at 12:36