Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

516
Vistas
How to retrieve object from JSON in nodejs?

Having this code:

const fs = require('fs')

const file = 'books.json';

class Book{
  constructor(code) {
    this._code = code;
  }

  get code() {
    return this._code;
  }

  set code(value) {
    this._code = value;
  }
}

async function writeBooks(){
  const data = JSON.stringify([new Book('c1'), new Book('c2')]);
  await fs.promises.writeFile(file, data, 'utf8');
}

async function getBook(code){
  try{
    const data = await fs.promises.readFile(file);
    const array = JSON.parse(data);
    return array.find(b => b.code === code);
  } catch (err){
    console.log(err)
  }
}

writeBooks();
getBook('c1').then(b => console.log(b));

I am getting undefined (instead of the expecting book object).

  1. How to get the object (the above problem)

  2. If async function always returns promise, how can I then return object for the client, instead of him having to call then() from the getBook(code)?

  3. do I need to await for the fs.promises.writeFile()? as I am doing in writeBooks()? As fas as I understand the async/await now, is that the return value from await function is the data or error. But since the writeFile() does not returns anything, or error at most (as opposed to readFile()) why would I want to await for no data?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Actually the root of problem is not about async/awaits or promises. The problem is trying to write an array to a json file. If you write your json data like the code snippet below (as a key-value pair), your problem is solved.

{"1": [new Book('c1').code, new Book('c2').code]} //as a key-value pair

const fs = require('fs')
const file = 'books.json';
class Book{
  constructor(code) {
    this._code = code;
  }
  get code() {
    return this._code;
  }
  set code(value) {
    this._code = value;
  }
}
async function writeBooks(){
  const data = JSON.stringify({"1": [new Book('c1').code, new Book('c2').code]});
  await fs.promises.writeFile(file, data, 'utf8');
}
async function getBook(code){
  try{
    const data = await fs.promises.readFile(file);
    const dataOnJsonFile = JSON.parse(data);
    return dataOnJsonFile["1"];
  } catch (err){
    console.log(err)
  }
}
writeBooks();
getBook('c1').then(b => console.log(b));

about 4 years ago · Juan Pablo Isaza Denunciar

0

  1. The above problem is that the Books returned from JSON.parse have only data, not methods, and thus I cannot get the code via get code(){}, but only as public parameter of class Book as book._code, which however breaks encapsulation (convetion is that _[propery] is private, and there should be appropriate getters/setters). So I made the properties public (and broke encapsulation), because I still don't know, how to assign methods to object created from JSON.

  2. No, the result of async is always Promise. You cannot unwrap it inside async, the client will always have to unwrap it. (so await fs.promises.WriteFile() will unwrap it, but then immediately wrap it back, before async function returns.

  3. as explained above.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda