“Uncaught (in promise) undefined” error when using with=location in Facebook Graph API query

前端 未结 2 1687
你的背包
你的背包 2020-12-15 15:58

I am currently developing a web application with the Facebook Graph API.

My current goal is to retrieve only posts which have a location attached.

While retr

相关标签:
2条回答
  • 2020-12-15 16:15

    The error tells you that there is an error but you don´t catch it. This is how you can catch it:

    getAllPosts().then(response => {
        console.log(response);
    }).catch(e => {
        console.log(e);
    });
    

    You can also just put a console.log(reponse) at the beginning of your API callback function, there is definitely an error message from the Graph API in it.

    More information: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch

    Or with async/await:

    //some async function
    try {
        let response = await getAllPosts();
    } catch(e) {
        console.log(e);
    }
    
    0 讨论(0)
  • 2020-12-15 16:29

    The reject actually takes one parameter: that's the exception that occurred in your code that caused the promise to be rejected. So, when you call reject() the exception value is undefined, hence the "undefined" part in the error that you get.

    You do not show the code that uses the promise, but I reckon it is something like this:

    var promise = doSth();
    promise.then(function() { doSthHere(); });
    

    Try adding an empty failure call, like this:

    promise.then(function() { doSthHere(); }, function() {});
    

    This will prevent the error to appear.

    However, I would consider calling reject only in case of an actual error, and also... having empty exception handlers isn't the best programming practice.

    0 讨论(0)
提交回复
热议问题