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

248
Visualizações
Sending data from MongoDB to my EJS file without refreshing the page?

Project goal: I am trying to create a language converter app. It will take a string from one textarea, convert all string values via a stored dictionary on a DB, and append this converted string into a second textarea. All of this conversion will be happening on the same index.ejs page (no refreshing/redirecting/etc.).

What works: I am able to POST the string data from my first textarea to index.ejs and retrieve all matching dictionary entries in my app.js. The "/" path (shown below) is my index.ejs file. The values are stored in an array called "convertArrOfArrs". I am using Mongoose to do this on my app.js. I have prevented the page from infinite refreshing (due to my POST request) via res.status(204).send().

What doesn't work: I can't figure out how to send this array of converted values ("convertArrOfArrs") to my index.ejs. I think res.render() doesn't work since I need to stay on the same page.

This is my app.js which deals with the POST request.

app.post("/", async (req, res) => {
  // entire textarea string
  const txtareaStr = await req.body.phoneticTxtArea;
  // puts string into an array, lowercases it, and removes whitespaces
  const txtareaArr = txtareaStr.trim().toLowerCase().split(" ");

  const convertArrOfArrs = [];
  try {
    for (let i = 0; i < txtareaArr.length; i++) {
      // this retrieves the word object based on the database match
      let word = await Word.findOne({ phonetic: txtareaArr[i] });
      // stores array of conversions for each word (ARRAY TO BE SENT!!!)
      convertArrOfArrs.push(word.converted);
    }
  } catch (e) {
    console.log("ERROR", e);
  }
  // prevents page from infinite refreshing after POST request
  res.status(204).send();
});

Please let me know if any other details are needed!

about 4 years ago · Juan Pablo Isaza
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