最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: Cannot read property 'id' of undefined in nodejs - Stack Overflow

programmeradmin1浏览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);
                    }

                }); 
            }

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);
                    }

                }); 
            }
Share Improve this question edited Sep 22, 2015 at 11:57 Ezekiel asked Sep 22, 2015 at 11:22 EzekielEzekiel 3334 gold badges9 silver badges27 bronze badges 10
  • Add console.log(data) in success and see what do you get in console – Tushar Commented Sep 22, 2015 at 11:23
  • It is not entering succes because the error is in server-side. – Ezekiel Commented Sep 22, 2015 at 11:26
  • So, you are using body parser middleware? , otherwise you will never get req.body on express. – Gonzalo Bahamondez Commented Sep 22, 2015 at 11:29
  • which express version you are using? – Gonzalo Bahamondez Commented Sep 22, 2015 at 11:31
  • Yes I am @GonzaloBahamondez. TTushar- it says in console undefined. And that's all. – Ezekiel Commented Sep 22, 2015 at 11:31
 |  Show 5 more ments

2 Answers 2

Reset to default 3

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 :)

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

发布评论

评论列表(0)

  1. 暂无评论