2

This question has been asked before but the question did not address the sending method and the solution marked does not work for me.

I am trying to get a boolean in a JSON object on the back end but I get a string instead.

I am sending an array of objects from axios with this:

const data = [{
  clientName: 'A client',
  columns: [{ 
    name: 'Keywords', 
    options: [{
      keyword: false,
      include: true
    }] 
  }]
}]

await axios.post('/config', { data })

But when I try to see what type are the options I get strings instead of booleans

On the back end I have this

server.js

import express from 'express'
import cors from 'cors'
import config from './routes/config.js'

const prod = process.env.NODE_ENV === 'production'
const app = express()

// Middleware
app.use(cors())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(express.static('uploads'))

app.use('/config', config)


// Start
const port = process.env.PORT || prod ? 5002 : 4003
app.listen(port, () => console.log(`Listening on port ${port}.`))

And the route

config.js


import { Router } from 'express'
import { writeFile } from 'fs/promises'


router.post('/config', async (req, res) => {
  const { data } = req.body.data

  // I want to write the data array with all of its objects however many they are, to file
  await writeFile(config.json, JSON.stringify(data)) 


  console.log(data)
}

export default router
[{
  clientName: 'A client',
  columns: [{ 
    name: 'Keywords', 
    options: [{
      keyword: 'false', // <-- string
      include: 'true'   // <-- string
    }] 
  }]
}]

############## UPDATE

My bad, I thought the react front end was sending booleans, it turns out they were actually strings

<option value={true}>Keyword</option>
<option value={false}>Domain</option>
Álvaro
  • 2,255
  • 1
  • 22
  • 48
  • Does this answer your question? [How do I consume the JSON POST data in an Express application](https://stackoverflow.com/questions/10005939/how-do-i-consume-the-json-post-data-in-an-express-application) – Boaz Jan 10 '22 at 17:29
  • Not really I am already using `app.use(express.json())` and`app.use(express.urlencoded({ extended: true }))` and yet they come back as strings – Álvaro Jan 10 '22 at 17:31
  • Please include the relevant part of the express app and middlewares. – Boaz Jan 10 '22 at 17:32
  • @Boaz I have added the main server file and its route – Álvaro Jan 10 '22 at 17:36
  • Inspect the headers of the axios request. You might be sending the wrong `Content-Type` header, so the result could not be processed as JSON but rather as url-encoded data. – Boaz Jan 10 '22 at 17:45
  • I have added expecifically `headers: { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8'}}` and stil getting string – Álvaro Jan 10 '22 at 17:49
  • This header will not preserve types such as boolean. Try `application/json`. This is also what axios is setting by default. – Boaz Jan 10 '22 at 17:51
  • this is the wrong app.use(express.urlencoded({ extended: true })) – rnewed_user Jan 10 '22 at 17:54
  • @Boaz It's fixed now, I did not have to add any headers, it was a mistake on how the front end was sending actulally a string, my bad – Álvaro Jan 10 '22 at 19:35

2 Answers2

1

Assuming the entire object is not a string and only the values are strings you can parse them.

JSON.parse(data[0].columns[0].options[0].keyword);
Finbar
  • 1,127
  • 1
  • 5
  • 17
  • But I want to write the entire array to file, how can I have a data array with all of its booelans and having to go one by one? – Álvaro Jan 10 '22 at 17:21
  • Can you update your question with more details on what youre trying to do? I answered for the issue of fields being strings not Booleans – Finbar Jan 10 '22 at 17:24
  • I have updated the question, the data array might have many columns and many options and I want to write it, with your anwser I would have to go one by one – Álvaro Jan 10 '22 at 17:26
0

i think this is the wrong

app.use(express.urlencoded({ extended: true }))

you are using urlencoded

you need to use bodyParser, do this to import body-parser

var bodyParser = require('body-parser');

and use that

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
rnewed_user
  • 1,386
  • 7
  • 13