如何修复UnhandledPromiseRejectionWarning

时间:2019-08-07 15:48:01

标签: node.js fs catch-block

我正在尝试纠正错误,但我不知道如何解决。我已经研究过寻找节点UnhandledPromiseRejectionWarning,但无法修复它。

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)
        });
  

(节点:18908)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ venue”       在C:\ Users \ Aaron Lanier \ gt \ sandbox \ liri-node-app \ liri.js:104:66       在process._tickCallback(内部/进程/next_tick.js:68:7)   (节点:18908)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。引发此错误的原因可能是抛出了一个没有catch块的异步函数,或者是拒绝了一个.catch()无法处理的承诺。 (拒绝ID:1)

     

(节点:18908)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。将来,未处理的承诺拒绝将以非零退出代码终止Node.js进程。

1 个答案:

答案 0 :(得分:0)

然后添加.catch方法。您还面临问题,因为未定义data [0]。请尝试控制台响应,以查看响应对象中哪些属性可用。