英文:
Proper RESTful API practice to run 2 queries in a function?
问题
目前我有这个,它完美地工作,除了它会导致我的Node.js服务器崩溃,出现以下错误。
Error: 无法在发送后设置标题。
有人可以帮助解决正确的语法吗?提前感谢。
英文:
So I'm developing an API for mySQL with nodejs. Trying to make a function that inserts into two different tables.
INSERT INTO account_details (username, first_name, last_name, gender, mobile_number, postal_code, email, activated) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
and
INSERT INTO passwords (username, password) VALUES (?, ?)
What's the proper way to do it?
Right now I have this, which works perfectly except it crashes my nodejs server, giving the error below.
addUser(request, respond) {
var userObject = new User(request.body.username, request.body.password, request.body.first_name, request.body.last_name, request.body.gender, request.body.mobile_number, request.body.postal_code, request.body.email, request.body.activated, null, null);
var sql = "INSERT INTO account_details (username, first_name, last_name, gender, mobile_number, postal_code, email, activated) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
var password = "INSERT INTO passwords (username, password) VALUES (?, ?)";
var values = [userObject.getUsername(), userObject.getFirstName(), userObject.getLastName(), userObject.getGender(),
userObject.getMobileNumber(), userObject.getPostalCode(), userObject.getEmail(), userObject.getActivated()];
var passvalues = [userObject.getUsername(), userObject.getPassword()];
db.query(sql, values, function (error, result) {
if (error) {
throw error;
}
else {
respond.json(result);
}
});
db.query(password, passvalues, function (error, result) {
if (error) {
throw error;
}
else {
respond.json(result);
}
});
}
Error: Can't set headers after they are sent.
Can anyone help with the proper syntax? Thanks in advance.
答案1
得分: 0
你可以将你的代码更改如下:
addUser(request, respond) {
var userObject = new User(request.body.username, request.body.password, request.body.first_name, request.body.last_name, request.body.gender, request.body.mobile_number, request.body.postal_code, request.body.email, request.body.activated, null, null);
var sql = "INSERT INTO account_details (username, first_name, last_name, gender, mobile_number, postal_code, email, activated) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
var password = "INSERT INTO passwords (username, password) VALUES (?, ?)";
var values = [userObject.getUsername(), userObject.getFirstName(), userObject.getLastName(), userObject.getGender(),
userObject.getMobileNumber(), userObject.getPostalCode(), userObject.getEmail(), userObject.getActivated()];
var passvalues = [userObject.getUsername(), userObject.getPassword()];
db.query(sql, values, function (error, result) {
if (error) {
throw error;
}
else {
db.query(password, passvalues, function (error, result) {
if (error) {
throw error;
}
else {
respond.json(result);
}
});
}
});
}
英文:
You could change your code as below:
addUser(request, respond) {
var userObject = new User(request.body.username, request.body.password, request.body.first_name, request.body.last_name, request.body.gender, request.body.mobile_number, request.body.postal_code, request.body.email, request.body.activated, null, null);
var sql = "INSERT INTO account_details (username, first_name, last_name, gender, mobile_number, postal_code, email, activated) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
var password = "INSERT INTO passwords (username, password) VALUES (?, ?)";
var values = [userObject.getUsername(), userObject.getFirstName(), userObject.getLastName(), userObject.getGender(),
userObject.getMobileNumber(), userObject.getPostalCode(), userObject.getEmail(), userObject.getActivated()];
var passvalues = [userObject.getUsername(), userObject.getPassword()];
db.query(sql, values, function (error, result) {
if (error) {
throw error;
}
else {
db.query(password, passvalues, function (error, result) {
if (error) {
throw error;
}
else {
respond.json(result);
}
});
}
});
}
答案2
得分: 0
因为你正在执行两次
respond.json(result);
第一个会成功,第二个会引发上面的错误。
你想要做的是等待它们都成功后再返回结果。
考虑使用Promises并等待这两个,然后继续响应。
英文:
Because you're doing it twice
respond.json(result);
first one will succeed and the second one will throw the above error.
what you want to do is to wait for both of them to succeed and return result.
Consider using Promises and await both of those and then proceed to response.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论