Intento actualizar el archivo JSON en el backend con express y mostrar nuevos datos en el frontend. Pero actualmente, cuando solicito, obtengo una buena respuesta, pero los datos no cambian.
Server Express (ruta) Script para editar JSON:
function editJSON(fr,en,es){ var obj = { list_words: [] } fs.readFile('./assets/words.json', 'utf-8', (err, jsonString) =>{ if(err){ console.log(err) }else { try{ obj = JSON.parse(jsonString) obj.list_words.push({fr: fr, en: en, es: es}) json = JSON.stringify(obj) fs.writeFileSync('./assets/words.json',json,'utf-8',function (err){ if (err) throw err; console.log('complete'); }) } catch (err){ console.log(" Error parsing JSON", err) } } }) }Y debajo del script para enviar datos y otros para mostrar.
methods: { async addWord () { var obj = { fr: this.words_input.fr, en: this.words_input.en, es: this.words_input.es } const res = await axios.post('http://localhost:3000/addwords', obj) const data = res.data console.log(data) } }, mounted: async function () { await axios.get('http://localhost:3000/words.json') .then(response => { this.words_init = response.data }) .catch(error => { console.log('There was an error: ' + error.response) }) }Si reinicio el servidor después de que se muestre la tabla de solicitudes con nuevos datos. Si sabe cómo puedo mostrar información sin reiniciar el servidor, esto me puede ayudar mucho.
probé esto
const res = await axios.post('http://localhost:3000/addwords', obj) const data = res.data.list_words this.words_init = dataCon esta ruta
app.post('/addwords',(req,res)=>{ console.log(req.body.fr) const fr = req.body.fr const en = req.body.en const es = req.body.es editJSON(fr,en,es) console.log(fr) res.send(words) res.status(201).send('created User') res.end() })pero tengo un error
"Error [ERR_HTTP_HEADERS_SENT]: no se pueden establecer encabezados después de enviarlos al cliente"
Desde atrás
y
"[Advertencia de Vue]: Error en el controlador v-on (Promise/async): "Error: Solicitud abortada"
Desde la interfaz
const res = await axios.post('http://localhost:3000/addwords', obj) this.words_init = res.data.list_words ``` with this route everything work But mu new list don't displayDebe devolver el JSON más reciente en la respuesta post y luego asignarlo a la variable this.words_init .