Discussions

Expand all | Collapse all

Uncaught TypeError: JSON.Stringify is not a function

  • 1.  Uncaught TypeError: JSON.Stringify is not a function

    Posted 26 days ago
    Hello,
    I'm trying to utilize the new REST API and am getting an error when I call the below script, which I've put in a code page. Am I missing something simple here?
    ---
    var headers = {
    'QB-Realm-Hostname': '{OUR REALM}',
    'Authorization': 'QB-USER-TOKEN {MY USER TOKEN}',
    'Content-Type': 'application/json'
    }
    var body = {
    "from": "{MY TABLE}",
    "select": [
    20,
    22,
    23,
    24,
    25,
    29,
    31,
    33
    ],
    "where": "{'6'.Ex.'Yes'}AND{'20'.Ex.'New York'}"
    }

    fetch('https://api.quickbase.com/v1/records/query',
    {
    method: 'POST',
    headers: headers,
    data: JSON.Stringify(body)
    })
    .then(res => {
    if (res.ok) {
    return res.json().then(res => console.log(res));
    }
    return Promise.reject({ status: res.status, statusText: res.statusText });
    })
    .catch(err => console.log(err))
    ---

    ------------------------------
    Christopher Wheatley
    ------------------------------


  • 2.  RE: Uncaught TypeError: JSON.Stringify is not a function

    Posted 25 days ago
    Christopher,
    Two problems - it's JSON.stringify (no caps)
    Also you need to add a content-length header (value is the length of the stringified body)
    Neil

    ------------------------------
    Neil Schneider
    ------------------------------



  • 3.  RE: Uncaught TypeError: JSON.Stringify is not a function

    Posted 25 days ago
    Thanks, Neil!  I've updated the code and now I'm getting a 400 error:

    "https://api.quickbase.com/v1/records/query 400 (Bad Request)"

    I've reached out to QuickBase support, hopefully they can help.  The original code came from their API guide so they're now aware of the typo.

    Best,

    Chris

    ------------------------------
    Christopher Wheatley
    ------------------------------



  • 4.  RE: Uncaught TypeError: JSON.Stringify is not a function

    Posted 25 days ago
    Chris,
    Did you add the Content-Length header?  You'll get an error 400 if that header isn't present.
    Also I'm assuming you are replacing the curly bracket enclosed items with the actual values, for example:
    'QB-Realm-Hostname': yoursite.quickbase.com
    'Authorization': 'QB-USER-TOKEN xxxxxx_xxxx_xxxxxxxxxxxxxxxxxxxxxxxxxx',  -where xxx... is a token for your app
    "from": "XXXXXXXXX",  - where XXX... is the internal table name
    Neil

    Neil

    ------------------------------
    Neil Schneider
    ------------------------------



  • 5.  RE: Uncaught TypeError: JSON.Stringify is not a function

    Posted 25 days ago
    Neil,

    I did. The top section is now like below. Info in {XXXXXX} does contain the real values in the code page, I've obscured them here for security:

    var body = {"from":"{XXXXXXXX}","select":[20,22,23,24,25,29,31,33],"where":"{'6'.Ex.'Yes'}AND{'20'.Ex.'New York'}"};

    var headers = {
    'QB-Realm-Hostname': '{XXXXXXXX}',
    'Authorization': 'QB-USER-TOKEN {XXXXXXXX}',
    'Content-Length': body.length,
    'Content-Type': 'application/json'
    }

    ------------------------------
    Christopher Wheatley
    ------------------------------



  • 6.  RE: Uncaught TypeError: JSON.Stringify is not a function

    Posted 25 days ago
    Hi Neil,

    I figured it out after a response from QuickBase support.  The 'data' key in the fetch() method should be 'body' not 'data' now it works great!  I tried with and without the Content-Length header and both ways are working.  Thanks for your help!

    ------------------------------
    Christopher Wheatley
    ------------------------------



  • 7.  RE: Uncaught TypeError: JSON.Stringify is not a function

    Posted 25 days ago
    No problem.  If you're doing a lot of testing you might want to take a look at Postman, it lets you set up and test an API without any coding...

    ------------------------------
    Neil Schneider
    ------------------------------



  • 8.  RE: Uncaught TypeError: JSON.Stringify is not a function

    Posted 25 days ago
    Chris,
    The length needs to be the length of the string created by JSON.stringify.  Try this:
    var body = {"from":"{XXXXXXXX}","select":[20,22,23,24,25,29,31,33],"where":"{'6'.Ex.'Yes'}AND{'20'.Ex.'New York'}"};
    var bodyToSend = JSON.stringify(body);
    ....
    'Content-Length' : bodyToSend.length
    ...
    data: bodyToSend






    ------------------------------
    Neil Schneider
    ------------------------------