Apologies for a potentially very simple question, I am very new to javascript and SQLite. I am building a webpage and on the server-side I have a database that stores all the users, their hashed passwords, sessionid, etc.
The table "Writers" consists of four columns: username, password (this is hashed, using bcrypt), sessionid, and socketid.
When a new user is registered I insert into the database, using this:
async registerNewWriter(username, password) {
console.log("IN REG NEW WRITER", username, password);
const saltRounds = 10; // default
bcrypt.hash(password, saltRounds, async (err, hash) => {
if (err) {
throw new Error(err);
}
await this.database.get(
"INSERT INTO Writers (username, password, sessionid) VALUES (?,?,?)",
`${username}`,
`${hash}`,
`null`,
);
// return hash;
});
}
After this, a session is created for the user. I update the database using this:
async createSession(id, socketID, username) {
console.log('create session', id, socketID, username);
await this.database.get(
"UPDATE Writers SET sessionid = ?, socketid = ? WHERE username = ?",
`${id}`,
`${socketID}`,
`${username}`, (err) => {
if (err) {
throw new Error(err);
}
}
);
}
Here is where I get the error: [Error: SQLITE_RANGE: column index out of range].
I have checked many similar questions online, but have not found the solution to this issue in particular. I have also made sure to check that sessionid, socketID and username all come into createSession with the correct values, but I do not understand why the database isn't being updated correctly.
If you know what could be wrong or have any suggestions, I will be very grateful! Thank you :)
CodePudding user response:
Parameters are quoted and scaped as need by default, may this could be your problem. Try remove those quotes like this:
async createSession(id, socketID, username) {
console.log('create session', id, socketID, username);
await this.database.get(
"UPDATE Writers SET sessionid = ?, socketid = ? WHERE username = ?",
id,
socketID,
username, (err) => {
if (err) {
throw new Error(err);
}
}
);
CodePudding user response:
If you are using an async version, you don't need (and probably can't use) a callback :
some_result = await this.database.get(
"UPDATE Writers SET sessionid = ?, socketid = ? WHERE username = ?",
`${id}`,
`${socketID}`,
`${username}`
}
);