Search code examples
javascriptmysqlexpressknex.js

Why does it say undefined?


I don't understand why "element" is undefined. I've used the debug but I was not able to find any information why it's undefined. Does somebody have any idea what's wrong?

Here's my code:

  const { id } = req.params; //id = 2
  const [billets] = await knex.raw('SELECT * FROM billet'); //RowDataPocket(3)
  const element = billets //undefined
    .find((billet) => billet.id === id);
  res.send(mainHTML(`<h1 class="title"> ${element.titre} </h1> <p>${element.texte}</p>`));

Solution

  • So I did this to solve my problem. So basicly it was an array list inside an array list so I had to precise wich one I wanted.

      const { id } = req.params;
      const [billets] = await knex.raw(`SELECT * FROM billet WHERE id = '${id}'`);
      const billet = billets[0];
      console.log(billet);```