无法读取未定义的属性,但可以将其记录到控制台

时间:2019-08-02 14:40:05

标签: node.js express post telegram-bot

我正在尝试从Express中的发布请求中检索数据,并将该数据传递给函数,一旦功能丰富了该数据,该函数便会以发布请求做出响应。

由于某种原因,请求失败,可能是由于TypeError:“无法读取未定义的属性'text'”

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

我已经重写了一些代码,它曾经有很多承诺,并在其中异步/等待。我认为这可能导致了这个问题,但是我无法弄清楚为什么仍然存在冲突。

// 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)

2 个答案:

答案 0 :(得分:0)

您在体内发送“ chat_id”,但尝试读取chat.id,因此您应该发送@if(count($users)) <header><h1>Users results</h1></header> @foreach ($users as $user) @include('user.userblock') @endforeach @endif @if(count($groups)) <header><h1>Groups results</h1></header> @foreach ($groups as $group) @include('group.gruopblock') @endforeach @endif @if(count($posts)) <header><h1>Posts results</h1></header> @foreach ($posts as $post) @include('post.postblock') @endforeach @endif

答案 1 :(得分:0)

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

运行getForecast.forecast后,“常量预测”的值是什么?