"Uncaught (in promise) undefined" error when using with=location in Facebook Graph API query
Asked Answered
R

3

85

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 retrieving posts with and without location is already working, I am not able to retrieve only posts with location.

The query which retrieves both types looks like this: '/me/feed?fields=id,name,message,picture,place,with_tags&limit=100&with=location'

The query which should retrieve only posts with location looks like this: /me/feed?fields=id,name,message,picture,place,with_tags&limit=100&with=location

The problem I have is that with the parameter &with=location I get an error Uncaught (in promise) undefined at this part of my code:

if (response.paging && response.paging.next) {
    recursiveAPICall(response.paging.next);
  } else {
    resolve(postsArr);
  }
} else {
  // Error message comes from here
  reject();
}

Log shows the following:

DEBUG: -------------------------------
DEBUG: Ember             : 2.4.5
DEBUG: Ember Data        : 2.5.3
DEBUG: jQuery            : 2.2.4
DEBUG: Ember Simple Auth : 1.1.0
DEBUG: -------------------------------
Object {error: Object}
  error: Objectcode: 
    code: 1
    1fbtrace_id: "H5cXMe7TJIn"
    message: "An unknown error has occurred."
    type: "OAuthException"
    __proto__: Object
  __proto__: Object
Uncaught (in promise) undefined

Does anyone have a possible solution for this?

For further information how the code looks like see my previous question.

Resoluble answered 3/6, 2016 at 22:24 Comment(3)
put a console.log(response) before that code part and see what you get. the code is very clear, you don´t catch an error in your current code. it most likely goes to the reject path.Infarct
I have done a bit more debugging now as you suggested. Indeed all the data was fetched. I also tried the same thing in the Graph API Explorer again. It seems there is a bug in the Graph API because even if there are no further posts there is the next property for pagination available. If you go to the link in the next property you get exactly the error message from above.Resoluble
I've had this error when my ajax call returned malformated JSONStyrax
I
157

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);
}
Infarct answered 4/6, 2016 at 8:18 Comment(4)
Finally, I did the recursion with the previous property instead of the next property which fixed the error at all.Resoluble
Where would you place the getAllPosts() ?Colorist
well....right where you need to get the posts? i am not sure if i understand your question.Infarct
what shows undefined? you need to be more specific, i assume something else is wrong in your code, but we do not know your code. so why the downvote?Infarct
E
21

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.

Elyse answered 5/1, 2017 at 9:55 Comment(2)
Oh, thank you for the "consider calling reject only in case..." partBatt
The 2nd parameter in the promise in then function is the reject propertyGuinevere
M
1
  1. Open Chrome Dev Tools
  2. Open Sources tab
  3. Make sure Debugger panel is open (the right most sidebar), otherwise expand it using this button:
    enter image description here
  4. In the Breakpoints section, check both boxes to pause on exceptions enter image description here
  5. Reload the page and make any additional steps to trigger the error
  6. Once the debugger is paused, inspect the code. Sometimes if you're lucky, it will be descriptive enough, or there will be a console log just before reject() kicks in.
Malodorous answered 6/12, 2023 at 10:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.