适当的RESTful API实践是在一个函数中运行2个查询吗?

huangapple go评论81阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年1月6日 14:46:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/59607784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定