Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

521
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda