【问题标题】:nodejs refresh html after fetch promise stays pending获取承诺后nodejs刷新html保持未决
【发布时间】:2019-12-23 21:42:39
【问题描述】:

我正在使用 express 和 nodejs,但在记录一个未决的承诺时遇到了问题。

      edit().then(data => console.log(data));

这里是编辑功能。

    async function edit(data, id) {
        let response = await fetch(config_url+'/subscriber/' + id, {
                headers : { "content-type" : "application/json; charset=UTF-8"},
                method: 'PUT',
                body: JSON.stringify(data)
            });
        let ret = await repsonse.json();
        return ret; 
    }

其余api为express js。

Subscriber.edit = function (name, email, id, result) {
        sql.query("UPDATE subscribers SET name=?, email=? WHERE id = ?", [name, email, id], function (err, res) {
            if (err) {
                console.log("error: ", err);
                result(null, err);
            } else {
                console.log(res);
                result(res);
            }
        });
    };

数据库中的数据发生变化,但在邮递员中 res.send() 行“订阅者更改成功”下方。

    exports.edit_subscriber = function (req, res) {
        console.log(req.params);
        console.log(req);
        console.log(res);
        Subscriber.edit(req.body.name, req.body.email, req.params.id, function(err, subscriber) {
            if (err) {
                res.sendStatus(err);
            }
            res.send({ message: 'Subscriber succesfully edited'});
        });
    };

为什么我自己的异步函数返回一个未解决的 Promise 并在 chrome 的控制台中保持挂起。

表达错误

    'access-control-allow-origin': [ 'Access-Control-Allow-Origin', '*' ]
  }
}
OkPacket {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 0,
  serverStatus: 2,
  warningCount: 0,
  message: '(Rows matched: 1  Changed: 1  Warnings: 0',
  protocol41: true,
  changedRows: 1
}
/Users/[classified]/repos/[classified]]/node_modules/mysql/lib/protocol/Parser.js:437
      throw err; // Rethrow non-MySQL errors
      ^

RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: OkPacket {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 0,
  serverStatus: 2,
  warningCount: 0,
  message: '(Rows matched: 1  Changed: 1  Warnings: 0',
  protocol41: true,
  changedRows: 1
}
    at ServerResponse.writeHead (_http_server.js:241:11)
    at ServerResponse._implicitHeader (_http_server.js:232:8)
    at write_ (_http_outgoing.js:607:9)
    at ServerResponse.end (_http_outgoing.js:717:5)

【问题讨论】:

  • 尝试在你的 express 代码中使用 async await,然后告诉我
  • @warmachine 它仍然会更改数据库,但 chrome 显示“PUT localhost:3000/subscriber/41 net::ERR_CONNECTION_REFUSED”
  • 表示express server 没有运行mate
  • @arunK 我添加了 express 错误。
  • 我发现了一些问题并在我的回答中发布了

标签: javascript node.js express es6-promise


【解决方案1】:

你必须对res.send(JSON.stringify(json))进行字符串化

exports.edit_subscriber = function (req, res) {
    ...
    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify({ message: 'Subscriber succesfully edited' }));
};

同样在下面的代码中,您将参数错误地传递给回调。第一个参数是err,第二个参数是result。它没有正确通过。

Subscriber.edit = function (name, email, id, resultCallBack) {
  sql.query("UPDATE subscribers SET name=?, email=? WHERE id = ?", [name, email, id], function (err, res) {
    if (err) {
      console.log("error: ", err);
      resultCallBack(err);
    } else {
      console.log(res);
      resultCallBack(null, res);
    }
  });
};

同样res.sendStatus 只接受状态码。

res.sendStatus(400).send(JSON.stringify{message: 'error occured'}); 

希望对你有帮助

【讨论】:

  • 我不能使用resultCallBack(找不到)但是缺少的else语句使我的服务器不会在每次发生错误时崩溃。 sendStatus(400) 和其他人一样向我解释了一些东西,非常感谢。问题更多的是then(data => console.log(data)); 没有发生,因为我认为记录时数据是空的。它不会等到它存在。
猜你喜欢
  • 1970-01-01
  • 2019-09-29
  • 1970-01-01
  • 1970-01-01
  • 2017-04-27
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
  • 2020-03-16
相关资源
最近更新 更多