Search code examples
javascriptjsonnode.jsexpresshandsontable

Can't access returned json data from handsontable


I have the following data being sent back to my Node/Express/Body-parser backend from a HandsOnTable.

[["Bob",null,"PhD",null],["Jane",null,"Masters",null],["Stew",null,"Degree",null]]

I'd like to be able to access each record (row) of data e.g. Bob, null, "PhD", null. This would give me my data to write out a document in Node.

When I do a console.log(req.body.table) I get all of the data above - if I do a console.log(req.body.table[0]) I only get a single '['.

Any ideas?


Solution

  • req.body.table is a string, that's why you get the first character only when doing req.body.table[0].

    Parse it first

    var parsed = JSON.parse(req.body.table);
    
    var bob = parsed[0];
    

    or if you're only consuming JSON, you can set Node Bodyparser to do it for you

    app.use(bodyParser.json())