Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
137
rated 0 times [  143] [ 6]  / answers: 1 / hits: 14116  / 9 Years ago, tue, september 22, 2015, 12:00:00

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

});
}

More From » jquery

 Answers
1

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


[#33995] Monday, September 21, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaylinshayd

Total Points: 443
Total Questions: 104
Total Answers: 111

Location: Nauru
Member since Wed, Mar 29, 2023
1 Year ago
;