Code executing after return
Unanswered
Chinese perch posted this in #help-forum
Chinese perchOP
con.connect((err: any) => {
if (err) throw err;
var sql = "SELECT * FROM users WHERE enrno=? ";
con.query(sql, [enrno], (err: any, result: any) => {
if (err) {
console.error("Error executing query:", err);
} else {
if (brcrypt.compare(password, result[0]['password'])) {
console.log("in"+_status)
_status = 200
} else {
_status = 403
}
}
});
});
console.log(_status)
return NextResponse.json({
message: "Incorrect enrollment number or password" //this message will only be used if the status is 403
}, {
status: _status,
})The return is sending the status as 0. The query sets it later. I tried await on the connect as well, in hopes something might happen, but to no avail. Im tired of errors.
34 Replies
You should absolutely await any DB calls to start with. Also, is
brcrypt a typo?@Mozzy You should absolutely await any DB calls to start with. Also, is `brcrypt` a typo?
Chinese perchOP
sorry. I was just trying everything
@Mozzy You should absolutely await any DB calls to start with. Also, is `brcrypt` a typo?
Chinese perchOP
It is indeed a typo, but it doesn't matter, as the it's just a variable
Chinese perchOP
?
What is
con? From some db library?@Mozzy What is `con`? From some db library?
Chinese perchOP
mysql2
Looking at https://github.com/sidorares/node-mysql2/blob/HEAD/documentation/en/TypeScript-Examples.md, I don't see a
.connect() function on the connection object?I am fluent with sql( For the most part )
It returns the correct value
No idea what this blog is, but that isn't the official docs
@Mozzy No idea what this blog is, but that isn't the official docs
Chinese perchOP
Nor are those
@Mozzy No idea what this blog is, but that isn't the official docs
Chinese perchOP
they dont have docs
The error is not from the connection
The connection works
I tested it
It gets the db
@Chinese perch they dont have docs
They do have some documentation: https://www.npmjs.com/package/mysql2
Chinese perchOP
It returns the values
But that is not the issue
@Chinese perch Show me what it is returning.
The reason why your code returns before your DB call finishes is because you are not awaiting anything, or returning inside the DB callback.
The reason why your code returns before your DB call finishes is because you are not awaiting anything, or returning inside the DB callback.
@Mozzy <@949657817479012422> Show me what it is returning.
The reason why your code returns before your DB call finishes is because you are not awaiting anything, or returning inside the DB callback.
Chinese perchOP
It returns the users value after login
Show me the newest code
@Mozzy Show me the newest code
Chinese perchOP
api route
import { NextResponse } from "next/server";
import { connectToDb } from '@/utils/db'
import { redirect } from 'next/navigation'
const bcrypt = require('bcrypt')
export async function POST(req: Request): Promise<NextResponse> {
console.log('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee')
const res = await req.formData()
let enrno: string = String(res.get("enrno"));
let password: string = String(res.get("password"));
let con = connectToDb("rcampus")
let _status = 0
con.connect((err: any) => {
if (err) throw err;
var sql = "SELECT * FROM users WHERE enrno=? ";
con.query(sql, [enrno], (err: any, result: any) => {
if (err) {
console.error("Error executing query:", err);
} else {
if (bcrypt.compare(password, result[0]['password'])) {
console.log("in"+_status)
_status = 200
} else {
_status = 403
}
}
});
});
console.log(_status)
return NextResponse.json({
message: "Incorrect enrollment number or password" //this message will only be used if the status is 403
}, {
status: _status,
})
}If so, the reason is that your con.connect() is not awaited in any way
To solve it, you could return a response inside the callbacks. Or you could take a look at this: https://www.npmjs.com/package/mysql2#using-promise-wrapper
@Mozzy I guess this part is running before anything else finishes?
Chinese perchOP
correct
@Mozzy To solve it, you could return a response inside the callbacks. Or you could take a look at this: https://www.npmjs.com/package/mysql2#using-promise-wrapper
Chinese perchOP
I'll check it out tommorow
Thanks!
Some light reading: https://javascript.info/async-await