【问题标题】:cannot read property of undefined, but can log it to console无法读取未定义的属性,但可以将其记录到控制台
【发布时间】:2019-08-02 23:16:43
【问题描述】:

我正在尝试从 Express 中的发布请求中检索数据,并将该数据传递给一个函数,该函数在丰富该数据后以发布请求进行响应。

由于某种原因,请求无法通过,可能是由于 TypeError: "Cannot read property 'text' of undefined"

奇怪的是,我可以在传入的请求中访问相同的属性,并将其记录到控制台,上面的一行应该是无法读取该属性的。

我已经稍微改写了代码,它曾经有很多 Promise 和 async / await 。我认为这可能导致了这个问题,但我无法弄清楚为什么仍然存在冲突。

//app.js

app.use("/", routes);

//routes.js

routes.post("/", controller.postRequest);

//controller.js

app.use("/", middleware.jsonParse);

const postRequest = app.post("/", (req, res, next) => {
    console.log("request is in postRequest function");
    console.log(req.body.message.text);
    let msg = req.body.message.text;
    let chat_id = req.body.message.chat.id;
    if (msg.indexOf("/weather") > -1) {
        try {
            const forecast = getForecast.forecast;

            if (forecast) {
                console.log("found forecast");
                try {
                    sendMessage.sendMessage(forecast, chat_id);
                } catch (error) {
                    throw error;
                }
            }
        } catch (error) {
            console.log(error);
            throw error;
        }
    } else {
        try {
            sendMessage.sendMessage("I did not understand you", chat_id);
        } catch (error) {
            throw error;
        }
    }
    res.status(200).end();
});

//getForecast.js

const forecast = async () => {
    console.log("request is in forecast");
    try {
        const response = await axios.get(full_url);
        return response;
    } catch (error) {
        console.log(error);
    }
};

//sendMessage.js

const sendMessage = (forecast, chat_id) => {
    axios
        .post(config.telegram_base_url + "/sendMessage", {
            chat_id: chat_id,
            text: forecast
        })
        .then(response => {
            return response;
        })
        .catch(error => {
            console.log(error);
        });
};

这是我收到的错误消息。出于某种原因,在我得到一个错误之前,它似乎每次都要运行两次代码?

Server now listening on port 3000
request received
request got to router
request got to controller
request is in postRequest function
/weather
request received
request got to router
request got to controller
request is in postRequest function
(node:27408) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'text' of undefined
    at app.post (/Users/louissugar/Documents/projects/weather_bot/controller.js:17:31)
    at Layer.handle [as handle_request] (/Users/louissugar/Documents/projects/weather_bot/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/louissugar/Documents/projects/weather_bot/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/louissugar/Documents/projects/weather_bot/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/louissugar/Documents/projects/weather_bot/node_modules/express/lib/router/layer.js:95:5)
    at /Users/louissugar/Documents/projects/weather_bot/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/louissugar/Documents/projects/weather_bot/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/louissugar/Documents/projects/weather_bot/node_modules/express/lib/router/index.js:275:10)
    at next (/Users/louissugar/Documents/projects/weather_bot/node_modules/express/lib/router/route.js:127:14)
    at app.post (/Users/louissugar/Documents/projects/weather_bot/controller.js:12:2)
(node:27408) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

【问题讨论】:

  • req.body.message.text = '/weather'?如果是这样,您的 api 将被调用两次,一次带有正文,一次没有。如果它正在发送两个电话,请检查您的前端。在浏览器的开发工具中
  • @Jazib 是的,文本是“/天气”。不过我没有前端,因为这是电报机器人的一部分

标签: node.js express post telegram-bot


【解决方案1】:

您在正文中发送“chat_id”,但尝试读取chat.id,因此您应该发送chat:{id:xxx}

【讨论】:

  • 我将来自请求的chat.id 存储在controller.js 中第7 行的变量chat_id 中('let chat_id = req.body.message.chat.id;')并传递它稍后进入块中的 sendMessage 方法
【解决方案2】:

为什么我没有看到控制台从 //getForecast.js 注销“预测中的请求”

getForecast.forecast 运行后 'const forecast' 的值是多少?

【讨论】:

    猜你喜欢
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 2015-11-14
    • 2016-08-10
    相关资源
    最近更新 更多