admin管理员组文章数量:1356940
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)
insuccess
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
2 Answers
Reset to default 3Make sure you've require
d 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
本文标签: javascriptTypeError Cannot read property 39id39 of undefined in nodejsStack Overflow
版权声明:本文标题:javascript - TypeError: Cannot read property 'id' of undefined in nodejs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744028535a2578480.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论