0
votes

I am building a web application using Parse.com as my backend. I have run into a problem when trying to store the response from Facebook's user profile API in a Parse database.

    FB.api('/me', function(response) {
        // Print Response to Console
        console.log(response);

        // Create New Parse Object
        var Facebook = Parse.Object.extend("Facebook");
        var userFacebook = new Facebook();
        for(var element in response) {
            userFacebook.set(element, response[element]);
        }

        // Store Data on Parse
        userFacebook.save(null, {
            success: function(user) {
            console.log("User Facebook data saved.");
        },
        error: function(user, error) {
            console.log("Facebook data storage error: " + error.code + " (" + error.message + ")");
        }
    });

The API response prints correctly to the console, and I receive two error messages, one after the other:

Failed to load resource: the server responded with a status of 404 (Not Found) https://api.parse.com/1/classes/Facebook/myID

and

Facebook data storage error: 101 (object not found for update)

In my Parse account, a database titled "Facebook" is created. However, it only contains a header for each of the elements in the response object (e.g., first_name, gender, email, etc.). It does not have a new row with these values.

I am really stuck on this one -- any help would be appreciated!

Note that the response object is in the form:

    {
        "email":"[email protected]",
        "first_name":"myFirstName",
        "gender":"myGender",
        "id":"myID",
        "last_name":"myLastName",
        "link":"https://www.facebook.com/app_scoped_user_id/myID/",
        "locale":"en_US",
        "name":"myFullName",
        "timezone":-7,
        "updated_time":"2014-03-12T04:57:39+0000",
        "verified":true
    }
2
If you need any more information, please do not hesitate to ask. Thanks! - Jesse

2 Answers

1
votes

The object in for each loop

    for(var element in response) {
       userFacebook.set(element, response[element]);
    }



    FB.api('/me', function(response) {
        // Print Response to Console
        console.log(response);

        // Create New Parse Object
        var Facebook = Parse.Object.extend("Facebook");
        var userFacebook = new Facebook();
        for(var element in response) {
            userFacebook.set(element, response[element]);
        }

        // Store Data on Parse
        userFacebook.save(null, {
            success: function(user) {
            console.log("User Facebook data saved.");
        },
        error: function(user, error) {
            console.log("Facebook data storage error: " + error.code + " (" + error.message + ")");
        }
    });
0
votes

The "id" column on a Parse.Object is protected and cannot be set via the API. In my case, Facebook's API response includes an "id" key, which is colliding with the Parse.Object "id" key.

If you are running into the same problem, you simply need to change the name of the "id" column. You can do this with the following code:

    for(var element in response) {
        if(element=="id") {
            userFacebook.set("userID", response[element]);
        } else {
            userFacebook.set(element, response[element]);
        }
    }

Thanks to Hector Ramos at Parse for helping me solve the problem! And thanks to Wayne for getting me part of the way!