I am new in node.js and javascript except CRUD Operations. I want to create a rest api with node.js with postgresql database, so that the methods PUT POST UPDATE DELETE work through JSON. Sory if my question is not clear but here is what i have, and what i cannot accomplish.
apiRoutes.get('/db/:id', function(request, response){
pg.connect(process.env.DATABASE_URL, function(err, client, done) {
client.query('SELECT * FROM test2 WHERE $1 = test2.id;', [request.params.id], function(err, result) {
done();
if (err){
console.error(err); response.json({success:"false", message: err});
}
else{
response.json({success: "true", data: result.rows} );
}
});
});
});
and here is the code which is to update the data on database through PUT Method.
apiRoutes.put('/db/:id', function(request, response){
var data1 = {name: request.body.name};
client.query('UPDATE test2 SET name=($1) WHERE id=($2)', [data1.name], [request.params.id]);
pg.connect(process.env.DATABASE_URL, function(err, client, done) {
client.query('SELECT * FROM test2 WHERE $1 = test2.id', [request.params.id], function(err, result) {
done();
if (err){
console.error(err); response.json({success:"false", message: err});
}
else{
response.json({success:"true", data: result.rows} );
}
});
});
});
When i click PUT in Postman, first it displays the data, including the name, but it deletes from the database, then even if i click raw then edit data, then send it, it wont save that, it returns to null, name = null. Sory if i havent been clear, but if someone understands me just a bit, a little help would be great, and this is important for me, to finish this. Thank you.