0
votes

I am trying to fix my error but i don't know how. i have researched around looking for node UnhandledPromiseRejectionWarning but i haven't been able to fix it.

function getBandsInTown(artist) {

    var artist = userInput;
    var bandQueryURL = "https://rest.bandsintown.com/artists/" + artist + "/events?app_id=codingbootcamp"

    axios.get(bandQueryURL).then(
        function (response) {
            console.log("=============================");
            console.log("Name of the venue: " + response.data[0].venue.name + "\r\n");
            console.log("Venue Location: " + response.data[0].venue.city + "\r\n");
            console.log("Date of event: " + moment(response.data[0].datetime).format("MM-DD-YYYY") + "\r\n");

            var logConcert = "======Begin Concert Log Entry======" + "\nName of the musician: " + artist + "\nName of the venue: " + response.data[0].venue.name + "\nVenue location: " + response.data[0].venue.city + "\n Date of event: " + moment(response.data[0].datetime).format("MM-DD-YYYY") + "\n======End Concert Log Entry======" + "\n";

            fs.appendFile("log.txt", logConcert, function (err) {
                if (err) throw err;
            })

            // logResults(response.data)
        });

(node:18908) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'venue' of undefined at C:\Users\Aaron Lanier\gt\sandbox\liri-node-app\liri.js:104:66 at process._tickCallback (internal/process/next_tick.js:68:7) (node:18908) 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)

(node:18908) [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.

1
add catch after then. ex: - axios.get(url).then(----).catch(err=>{ console.log(err); }); - rainyday
also useful console.log(response); right after function (response) { - GrafiCode
This is not the cause of the error you write about, but this if (err) throw err; inside a plain async callback is NEVER appropriate code. It does nothing useful. Write real error handling. - jfriend00

1 Answers

1
votes

Add .catch method after then. also you are facing issue because data[0] is not defined. please try to console response to see which are properties are available to you in response object.