I've been working with node.js with sqlite3 database. Below is one of my endpoints to get data from the database.
app.get('/newGame', async (req, res) => {
try {
let name = req.query.name;
let size = req.query.size;
let db = await getDBConnection();
let insertGameQry = 'INSERT INTO games (winner_id) VALUES (null);';
let insertGameResult = await db.run(insertGameQry);
let gameId = insertGameResult.lastID;
res.json({'game_id': gameId});
} catch (err) {
res.type('text');
res.status(SERVER_ERROR).send(SERVER_ERROR_MSG);
}
}
I first get the two query params from the request and connect to the database by getDBConnection()
, and then insert a null value to winner_id
of the table games
(since there's still not a winner), and thus get the gameId
in the same table which auto increments by using lastID
. However, I constantly get a server 500 error when I clicked the corresponding button at the client side. I've included all the dependencies and confirmed to have the correct project structure and a .db
file. Is there any way to get where the problem is? I've also tried to console.log
the query parameters in google DevTool and it works fine. However, when I console.log
the gameId
, it prints nothing on the console. Can anybody provides a method for debugging?
Check your sqlite3 database schema to see if winner_id
has NOT NULL
set.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.