Answer the question
In order to leave comments, you need to log in
Why doesn't the callback in fs.readFile fire?
Why does err not work that the file does not exist ( file does not exist), but throws another error related to the else code
SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
fs.readFile(file, 'utf8', (err, snapshot) => {
if (err) {
axios.get(url)
.then((res) => {
let firstSnapshot = { data: '', date: '' }
let $ = cheerio.load(res.data)
firstSnapshot.data = $(selector).html().replace(/\s/g, '')
firstSnapshot.date = new Date(Date.now())
fs.writeFile(file, JSON.stringify(firstSnapshot, null, 2), (err) => {
return console.log('First snapshot file created!')
})
})
}
snapshot = JSON.parse(snapshot)
console.log(snapshot.date)
})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question