Node: UnhandledPromiseRejectionWarning

Created on 1 Aug 2018  路  3Comments  路  Source: nodejs/node

Hello everyone,

I'm having always this error when trying to execute my function, but i still don't get it:
(node:38) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'body' of undefined
(node:38) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

My current nodejs version is: 8.9.1
Here it is my code:

'use strict';

const rp = require('request-promise-native');

module.exports = async function (context) {
const stringBody = JSON.stringify(context.request.body);
const body = JSON.parse(stringBody);
const location = body.location;

if (!location) {
    return {
        status: 400,
        body: {
            text: 'You must provide a location.'
        }
    };
}

try {
    const response = await rp(`https://query.yahooapis.com/v1/public/yql?q=select item.condition from weather.forecast where woeid in (select woeid from geo.places(1) where text="${location}") and u="c"&format=json`);
    const condition = JSON.parse(response).query.results.channel.item.condition;
    const text = condition.text;
    const temperature = condition.temp;
    return {
        status: 200,
        body: {
            text: `It is ${temperature} celsius degrees in ${location} and ${text}`
        },
        headers: {
            'Content-Type': 'application/json'
        }
    };
} catch (e) {
    console.error(e);
    return {
        status: 500,
        body: e
    };
}

}

Please any help with this issue.

Thank you.

invalid question wrong repo

All 3 comments

Hi @white-cloud, it's hard to help given that you're not providing the full reproduction. It's unlikely this is an error in Node.js core given the usage of third-party libraries. The issue tracker here is strictly for bugs and issues with Node.js core, I would instead suggest opening an issue in https://github.com/nodejs/help/issues as it was created exactly for these types of questions.

@white-cloud You are probably calling the exported function from this your module without await or .catch and not providing a .request property in context argument. Hard to say anything else without full info.

Thank you for your answers @ChALkeR @apapirovski

Was this page helpful?
0 / 5 - 0 ratings

Related issues

addaleax picture addaleax  路  3Comments

sandeepks1 picture sandeepks1  路  3Comments

cong88 picture cong88  路  3Comments

dfahlander picture dfahlander  路  3Comments

vsemozhetbyt picture vsemozhetbyt  路  3Comments