POSTing to a REST API isn't working

Hi, hopefully I am posting in the correct forum thread, if not, let me know please. I am currently working on an app that, after being authenticated to Google’s Firebase (which is working), I want to then POST data to that same Firebase’s database via its API. See code below.

Here is where the function first gets called (added -----> to call attention to the function in question)

var createUser = function() {
    var email = userEmailInput.value;
    var password = userPasswordInput.value;
    EAuth.createWithEmailAndPassword(email, password).then(function(user) {
        signedIn();
-----> putUser();  <-------
        router.goto("home");

    }).catch(function(e) {
        console.log("Signup failed: " + e);
        FirebaseUser.onError(e, -1);
    });
};

that function when called then runs this bit of code

function putUser() {

    var url = encodeURI(ROOT_URL);

    fetch(url, {
        method: 'POST',
        headers: { "Content-Type": "application/json"},
        body: JSON.stringify(userInfo)
    }).then(function(response) {
        console.log("inreponse");
        return response.json();
----->   }).then(function(responseObject) {  <------
        console.log(JSON.stringify(responseObject));

        if (responseObject.object == 'token') {
            console.log("wemadeittohere");
         }

        if (responseObject.error) {
            console.log(" 1 Error: " + responseObject.error.message);
        }
    }).catch(function(err) {
        console.log("2 Error: " + err);
    });
    console.log("afterputUser");
}

I have a lot of console.log’s to help me track the process and see what works and what doesn’t. Everything works up until the responseObject line (again marked with -----> for emphasis). It skips it and runs the catch(function(err) instead, and I don’t know why. What am I supposed to do with that responseObject?

-All I get in the command prompt is an error that says “2 Error: SyntaxError: Unexpected token a in JSON at position 0”.

Thank you for your help in advance! If anything is unclear, let me know!

Not sure but if you take a look at this below example on github. It might help you

SyntaxError: Unexpected token a in JSON at position 0 means that your fetch request returned something that isn’t JSON, since that error is most probably thrown at

return response.json();

What you could do is log what the response actually is, to see what you’re getting back from the service. E.g.:

console.log(JSON.stringify(response));
return response.json();

Hint: it will probably start with the letter “a”.

Uldis wrote:

SyntaxError: Unexpected token a in JSON at position 0 means that your fetch request returned something that isn’t JSON, since that error is most probably thrown at

return response.json();

What you could do is log what the response actually is, to see what you’re getting back from the service. E.g.:

console.log(JSON.stringify(response));
return response.json();

Hint: it will probably start with the letter “a”.

That worked! Thanks a ton! I literally just forgot to put .json on the end of the URL for the fetch… I had a feeling it was that, but I have worked a lot as an iOS developer, in Xcode/Swift, and I don’t remember needing to add that on.

Last question though, is there any way really for me to use Swift in Fuse?

Caleb Strong wrote:
Last question though, is there any way really for me to use Swift in Fuse?

Sure hope it isn’t the last question ever, but yes, there is.