18

I am able to successfully retrieve an object from parse using command below:

curl -X GET \
-H "X-Parse-Application-Id:1231231231" \
-H "X-Parse-REST-API-Key: 131231231" \
-G \
--data-urlencode 'where={"uid":"12312312"}' \
https://api.parse.com/1/classes/birthday`

But I'm struggling converting it to javascript, using the code below I will get response with a status of 200. I assume error is converting data-urlencode to data:

var getObject = function {
    var url = "https://api.parse.com";
    url += '/1/classes/birthday';

    fetch(url, {
        method: 'GET',
        headers: {
            'Accept': 'application/json',
            'X-Parse-Application-Id': '12122',
            'X-Parse-REST-API-Key': '12121',
            'Content-Type': 'application/json',
        },
        data: '{"where":{"uid":"12312312"}}'

    })
    .then(function(request, results) {
        console.log(request)
        console.log(results)
    })
}

Thanks!

4
  • 1
    I'm not familiar with fetch but it looks like there should not be single quotes around the data, like data:{"where":{"uid":"12312312"}} Commented Jun 25, 2015 at 1:16
  • @chiliNUT Just tried that, same response either way, thanks though: body: (... ), bodyUsed: false, headers: Headers, ok: true, status: 200, statusText: "OK", type: "cors", url: "https://api.parse.com/1/classes/birthday" Commented Jun 25, 2015 at 1:20
  • so did you fix this? im trying something similar, but i no matter what i put in data, even malformed json, i get all the items from class/Run. its not filtering anything, or giving me errors. when i do the same with curl, i get "json error", which is the correct thing to get Commented Jul 18, 2015 at 13:31
  • Why not just use wireshark.org (Wireshark) and see for yourself what's being sent, before trying to produce the same output? Commented Jun 14, 2016 at 8:41

1 Answer 1

7

Ok so basically you need another then. So it has something to do with promises. I figured it out after reading this awesome article http://blog.gospodarets.com/fetch_in_action/

What you need to do is this

var getObject = function {
  var url = "https://api.parse.com";
  url += '/1/classes/birthday';

  return fetch(url, {
      method: 'GET',
      headers: {
          'Accept': 'application/json',
          'X-Parse-Application-Id': '12122',
          'X-Parse-REST-API-Key': '12121',
          'Content-Type': 'application/json',
      },
      data: '{"where":{"uid":"12312312"}}'

  })
  .then(function(response) {
    return response.text();
  })
  .then(function(data){
    console.log(data); //this will just be text
    var data_obj = JSON.parse(data);
    return data_obj
  })

}

I don't really understand why you have to return response.text(). Again it has something to do promises

https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Promise

Because when I access response.text() from the first then. It returns a promise.

Hopefully some other kind gentleman can comment and explain why returning response.text() turns the promise into the data you need.

--EDIT--

I added some returns to show how you can have getObject return out the response data as an object. So

var d = getObject();

will return the response as a js object now.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes Charles! I love you :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.