I cannot insert data into the SQLITE3 table (NODE.JS)

Asked

Viewed 21 times

0

inserir a descrição da imagem aqui

Whenever I try to add data the SQLITE table appears this error. Someone helps me, pfv. I’m starting at the Back-end now ;-;

        const Database = require("../db/config")

        module.exports = {
            async create(req, res){
                const db = await Database()
                const pass = req.body.password
                let roomId

                /* Gera o numero da sala */
                for(var i = 0; i < 6; i++){
                    i == 0 ? roomId = Math.floor(Math.random() * 10).toString() :
                    roomId += Math.floor(Math.random() * 10).toString()
                }


                /* Inseri a sala no banco */
                await db.run(`INSERT INTO rooms (
                    id,
                    pass
                ) VAlUES (
                    ${parseInt(roomId)},
                    ${pass}
                )`)
                    

                await db.close()

                res.redirect(`/room/${roomId}`)
            }
        }
  • The error message (which you put in picture but should have put as text in question) says that db is undefined. Apparently, await Database() does not return anything. You need to see the code of this file.

  • I managed to solve, I redid my config file following the same logic and it worked. Anyway...kkkk will understand. But thanks for the help :)

1 answer

0

Doc, based on what you sent, I don’t know what’s in the '.. /db/config' but in this part of the code you sent, require was missing as for example below.

const sqlite3 = require('sqlite3');

const db = new sqlite3.Database('./database.sqlite', (error) => {
  if (error) console.log(error);
});

this was done inside the '.. /db/config'? I believe you have to bring the call here to your main file and leave only the setting as path... password... in this separate file.

What the error is telling you is that you don’t recognize this . run() IE, did not work out your require

  • put the config below , thank you p is trying to help me :)

  • but then @gabrielReis, he’s not able to pull the require from inside the file, put it all in a file just to see if you understand?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.