【发布时间】: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