0

I have a simple route for testing purposes which looks like this:

exports.looseAllPendingBets = (req, res) => {

    let array = JSON.stringify(req.body.arrayToDelete)
    console.log(array)
};

I then pass an array of object from my client like this:

<form action="http://localhost:4000/user/looseAllPendingBets" method="post">
        <input type="hidden" :value="selected" name="arrayToDelete">
        <button type="submit" class="btn btn-outline-danger">All selected = Lost</button>
</form>

The bind value :selected is an array of objects like this:

selected: [{"id": "61277276d8b2b2000482fc39", "amount": "16"}, {"id": "612772d3d8b2b2000482fc3c", "amount": "15" }]

But for some reason when I console.log(array) in the route, it gives me [object Object].

I have tried JSON.stringify() like shown above, and also array[0] and array[0].id but this gives me undefined.

Been struggeling with this for a while. Please note I'm still new to this type of development. Thank you.

egx
  • 389
  • 2
  • 14
  • Did you try to log `req.body.arrayToDelete` variable? (without `json.stringify` call) – Andrew Evt Aug 27 '21 at 07:16
  • Yes. Sorry the example should be console.log(array). I have changed it. But yes and it still gives me [object Object]. – egx Aug 27 '21 at 07:17
  • hm... what about `req.body` -> are you able to see the value of `arrayToDelete` parameter, or is it `[object Object]` there? – Andrew Evt Aug 27 '21 at 07:21
  • Yes. If I just log req.body I get [object Object] – egx Aug 27 '21 at 07:22
  • It seems like something is wrong with data transfer from the client or data-parsing on the backend side. I'd suggest checking 2 places: 1. Which data is being sent from the client and in which format? (is it a `form-data`, `json`, etc. You can see it in the browser) 2. How the data is parsed before you receive it in the `looseAllPendingBets` function? (usually, in node.js and express you have to have `body-parser` or something similar. Check the link for more details -> https://stackoverflow.com/questions/11625519/how-to-access-the-request-body-when-posting-using-node-js-and-express) – Andrew Evt Aug 27 '21 at 07:28
  • Did you try `req.body.arrayToDelete.data` ? – Nikola Pavicevic Aug 27 '21 at 07:46
  • Yes just tried. It is the same result... – egx Aug 27 '21 at 07:53
  • And this way `JSON.stringify(req.body.arrayToDelete.data)` ? – Nikola Pavicevic Aug 27 '21 at 08:09
  • The error means that you already botched the value that was provided to JSON.stringify. *The bind value :selected is an array of objects like this* - so you provided an array to text input. What is it supposed to look like? If you want to pass JSON then it should be `:value="JSON.stringify(selected)"` – Estus Flask Aug 27 '21 at 10:05
  • Thank you. This solve my problem! – egx Aug 31 '21 at 13:37

0 Answers0