【问题标题】:express deprecated res.send(status, body): Use res.status(status).send(body) does not work even when expresion changeexpress deprecated res.send(status, body):即使表达式发生变化,使用 res.status(status).send(body) 也不起作用
【发布时间】:2021-10-24 10:06:06
【问题描述】:

我正在尝试在我的 Express 应用程序的 POST 中返回 JSON。

我收到错误“express deprecated res.send(status, body): Use res.status(status).send(body)”,但即使我更改错误所在的行,它仍然显示相同的错误。

我的 package.json 是:

{
  "name": "Email",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@azure/cognitiveservices-face": "^4.2.0",
    "@azure/ms-rest-js": "^2.6.0",
    "axios": "^0.23.0",
    "express": "^4.17.1",
    "nodemailer": "^6.7.0"
  }
}

我用于快递的名称:

const express = require("express");
const bodyParser = require("body-parser");

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

我试图获得成功响应的代码是:

app.post("/analisis/faceid", (req, res) => {
  let img = req.body.img;

  axios({
    method: "post",
    url: endpoint_faceID,
    params: {
      detectionModel: "detection_03",
      returnFaceId: true,
    },
    data: {
      url: img,
    },
    headers: {
      "Ocp-Apim-Subscription-Key": Ocp_Apim_Subscription_Key,
    },
  })
    .then(function (response) {
      let response_data = {
          mgs: "OK",
        faceId: response.data[0].faceId
      };
      res.send(response_data); //HERE IS WHERE THE ERROR APPEARS 
    })
    .catch(function (error) {
      res.send("ERROR AT FACE ID : ", error);
    });
});

我已尝试将其替换为:

res.json(response_data)
res.jsonp(200, response_data)
res.status(200).send(response_data)
res.status(200).send(JSON.stringify(response_data))
res.status(200).json(response_data)
res.status(200).jsonp(response_data)

但我一直收到同样的错误。

PD:对不起我的英语

【问题讨论】:

  • 将其更改为res.json(response_data)(然后重新启动服务器)应该可以避免该错误。
  • 谢谢,但我已经使用它并得到同样的错误:(
  • 会不会是错误消息出现在res.send("ERROR AT FACE ID : ", error); 行,您使用两个 参数调用res.send?如果将其替换为 res.send(error);,您将看到根本错误。例如,这可能是 axios 发布请求失败。

标签: node.js json express


【解决方案1】:

谢谢大家。

问题是我试图传递的 2 个参数

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 2019-12-15
  • 2015-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-05
  • 1970-01-01
相关资源
最近更新 更多