1

I try to write data, but inside a file, I see [object object]. Why this is happened? I don`t understand All URLs are true

server.get('someurl', (req, res) => {
  const dataName = `${__dirname}/data.json`
  fs.readFile(dataName, { encoding: 'utf8' }, (err, data) => {
    if (!err) {
      return res.json(JSON.parse(data))
    }
    const url = 'anotherurl'
    const getData = async () => {
      const result = await axios(url).then((d) => d.data)
      return result
    }
    return fs.writeFile(dataName, { encoding: 'utf8' }, () => {
      res.json(getData)
    })
  })
})
Ivan Ch
  • 55
  • 6

1 Answers1

2

You are using fs.writeFile wrong it expects (path_to_file, data, encoding(optional), callback)

So in your case it would be

fs.writeFile(dataName, getData, 'utf8', () => {
    //callback
})
srosati
  • 118
  • 1
  • 10