Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
Cómo actualizar JSON con express y Vue

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 = data

Con 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 display
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Debe devolver el JSON más reciente en la respuesta post y luego asignarlo a la variable this.words_init .

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!