Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript nodejs在获取承诺保持挂起状态后刷新html_Javascript_Node.js_Express_Es6 Promise - Fatal编程技术网

Javascript nodejs在获取承诺保持挂起状态后刷新html

Javascript nodejs在获取承诺保持挂起状态后刷新html,javascript,node.js,express,es6-promise,Javascript,Node.js,Express,Es6 Promise,我使用的是express和nodejs,但在记录一个仍然挂起的承诺时遇到了问题 edit().then(data => console.log(data)); 这里是编辑功能 async function edit(data, id) { let response = await fetch(config_url+'/subscriber/' + id, { headers : { "content-type" : "a

我使用的是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; 
    }
restapi是expressjs

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);
            }
        });
    };
数据库中的数据发生了更改,但在postman中的res.send()行“subscriber changed successfully”下方

    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'});
        });
    };
同样,为什么我自己的异步函数返回一个承诺,但这个承诺没有得到解决,并且在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)

您必须将
res.send(JSON.stringify(JSON))

在下面的代码中,您将参数错误地传递给回调函数。第一个参数是
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'}); 

希望这有助于

在您的express代码中尝试使用async Wait,让我know@warmachine它仍然会更改数据库,但chrome显示“PUT net::ERR_CONNECTION_densed”,这意味着express server没有运行mate@arunK我添加了明示错误。我发现了一些问题,并在我的回答中发布了我无法使用
resultCallBack
(找不到)但是丢失的else语句使我的服务器不会在每次发生错误时崩溃。而且
sendStatus(400)
向我解释了一些东西,就像其他东西一样,对此非常感谢。问题更多的是
然后(data=>console.log(data))
不会发生,因为我认为记录数据时数据是空的。它不会等到数据存在。
res.sendStatus(400).send(JSON.stringify{message: 'error occured'});