节点js后端

时间:2018-02-15 09:43:04

标签: node.js ajax error-handling promise

我正在使用node.js后端,我在处理错误时遇到了一些问题。

在后端我使用express进行路由。我从前端获得了一个带有数组和一些数据的ajax帖子。该数据应保存在数据库中。如果通过将数据添加到DB而出现错误,我会在后端收到错误消息,但我也希望向前端发送消息。我试着错过,但在前端我总是'成功'。 这是我的代码,直到现在。

后端:

router.post('/tagging', function(req, res) {
  var taggedData = req.body;

  var actions = taggedData.map(element => {
    addTaggedData.addTaggedData(element)
      .then(function(result) {
        return result;
      })
      .catch(function(err) {
        if (err.code == "ER_NO_SUCH_TABLE") {
          console.log("Tagged data contains unknown project name");
          res.send("ER_NO_SUCH_TABLE");
        } else {
          res.send(err);
        }
      })
  });
  Promise.all(actions)
    .then(
      res.send("Successful")
    )
    .catch(function(err) {
      if (err.code == "ER_NO_SUCH_TABLE") {
        console.log("Tagged data contains unknown project name");
        res.send("ER_NO_SUCH_TABLE");
      } else {
        res.send(err);
      }
    });
})

前言ajax电话:

function postTaggedData(taggedData) {
$.ajax({
    url: server_connection.url + '/tagging',
    type: 'POST',
    encoding: "UTF-8",
    contentType: 'application/json',
    data: JSON.stringify(taggedData),
    success: function(data) {
        if (data === "Successful") {
            console.log("Tagged Data successfully send to server");
        }else if(data == "ER_NO_SUCH_TABLE"){
            alert("Unknown project");
        } else {
            alert(data);
        }
    },
    error: function(xhr, status, error) {
        if(error == "Internal Server Error"){
            alert("There is an error with the server");
        }else if(error == "ER_NO_SUCH_TABLE"){
            alert("Unknown project");
        }else{
            alert("There was an error while sending the Tagged Data to the server");
            console.log(xhr, "Status: ", status, error);
        }

    }
})

}

1 个答案:

答案 0 :(得分:1)

即使你发送错误作为回复,快递也不知道它是错误的,所以它发送的状态代码为res.status(404).send(err),这意味着好,所以前面 - 最终认为,回应是成功的。

尝试设置非正常状态,然后发送如下错误:404。其中Not Found是" > Configure project :TMessagesProj The setTestClassesDir(File) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use the setTestClassesDirs(FileCollection) method instead. The getTestClassesDir() method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use the getTestClassesDirs() method instead. The ConfigurableReport.setDestination(Object) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use the method ConfigurableReport.setDestination(File) instead. > Task :TMessagesProj:processArmv7DebugGoogleServices Parsing json file: /home/elanimus/Desktop/plus-messenger-master-1d96151861d0db96ffe384807621a0a3a2d371c8/TMessagesProj/google-services.json FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':TMessagesProj:compileArmv7DebugJavaWithJavac'. > java.lang.reflect.UndeclaredThrowableException * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. * Get more help at https://help.gradle.org BUILD FAILED in 48s 36 actionable tasks: 36 executed "

的状态代码

您可以找到有关状态代码here

的更多信息

您可以找到有关快速错误处理here

的更多信息