Node.js 使用nodejs更新postgresql中的查询
更新学生信息Node.js 使用nodejs更新postgresql中的查询,node.js,postgresql,Node.js,Postgresql,更新学生信息 function update_student(req, student, next, res) { console.log('----',student); if (Object.keys(student).length > 0) { var query = updateQuery('student', req, student, '_id'); var update_data = []; Object.keys(st
function update_student(req, student, next, res) {
console.log('----',student);
if (Object.keys(student).length > 0) {
var query = updateQuery('student', req, student, '_id');
var update_data = [];
Object.keys(student).forEach(function(key) {
update_data.push(student[key])
});
db.query(req, update_data).then(function(result) {
return res.send('Updated');
})
}
else {
return res.send('nothing to update');
}
};
更新查询
function updateQuery(tablename, id, data, condition) {
var query = ['UPDATE'];
query.push(tablename)
query.push('SET');
var set = [];
Object.keys(data).forEach(function(key, i) {
if ((key == 'university_id') || (key == 'name') || (key == 'branch') || (key == 'age') || (key == 'email')) {
set.push('"' + key + '"' + ' = $' + (i + 1));
} else {
set.push(key + ' = $' + (i + 1));
}
});
query.push(set.join(', '));
query.push('WHERE ' + condition + ' = ' + id);
return query.join(' ');
}
代码中有什么错误?我正在尝试使用node.js在postgreSQL中编写更新查询。我无法使用此查询更新student表。您是否收到任何特定错误,并且您是否尝试打印生成的查询字符串,以查看生成的更新是否看起来像有效的SQL。您应该将
。catch
处理程序添加到db.query
调用中,以处理任何错误,这会告诉你你的问题出在哪里。它显示在使用的每个示例中。它显示了postman中的401 not found错误