Reputation:
I'm making some inserts in my BD mysql, but the console is returning me a problem with the syntax of mysql.
async postCompletedetails(req, res) {
const company = req.params.company;
const name = req.params.name;
const password = req.params.password;
bcrypt.hash(password, saltRounds, (err, hash) => {
// Now we can store the password hash in db.
});
if (
company !== undefined &&
name !== undefined &&
password !== undefined
) {
const {
token
} = req.headers;
const decoded = jwt.verify(token, process.env.JWT_ACCOUNT_ACTIVATION);
const {
id
} = decoded;
const update = await pool.query(
`UPDATE user SET Name_user= '${name}', password= '${password}' WHERE ID_user = ${id}`
);
const insertid = await pool.query(
`INSERT INTO rel_company_user (ID_user) VALUES ${id}`
);
const incompany = await pool.query(
`INSERT INTO company (Name_company) VALUES '${company}' `
);
const inrelcompany = await pool.query(
`INSERT INTO rel_company_user (ID_company, ID_user) VALUES (LAST_INSERT_ID(), ${id})`
);
return res.json({
code: 200,
message: "todo bien... todo correcto y yo que me alegro",
password,
});
} else {
return res.json({
code: 400,
message: "Bro hiciste algo mal",
});
}
}
error from console:
(node:9892) UnhandledPromiseRejectionWarning: Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '36' at line 1
36 is correct, that user actually exist in My BD, so I don't have an idea what's syntactically wrong
I have a solution, I was declaring the id like an object, but the right is declared it in this method = const id = decode.id with that, I have the value, but now I have the next error:
node:16032) UnhandledPromiseRejectionWarning: Error: ER_NO_REFERENCED_ROW_2: Cannot add or update a child row: a foreign key constraint fails (`kiraldb`.`rel_company_user`, CONSTRAINT `rel_company_user_ibfk_1` FOREIGN KEY (`ID_company`) REFERENCES `company` (`ID_company`) ON UPDATE CASCADE)
It is for the first insert
Upvotes: 0
Views: 44
Reputation:
async postCompletedetails(req, res) {
const company = req.params.company;
const name = req.params.name;
const password = req.params.password;
bcrypt.hash(password, saltRounds, (err, hash) => {
// Now we can store the password hash in db.
});
if (
company !== undefined &&
name !== undefined &&
password !== undefined
) {
const { token } = req.headers;
const decoded = jwt.verify(token, process.env.JWT_ACCOUNT_ACTIVATION);
const id = decoded.id;
const update = await pool.query(
`UPDATE user SET Name_user= '${name}', password= '${password}' WHERE ID_user = ${id}`
);
const incompany = await pool.query(
`INSERT INTO company (Name_company) VALUES ('${company}') `
);
const inrelcompany = await pool.query(
`INSERT INTO rel_company_user (ID_company, ID_user) VALUES (LAST_INSERT_ID(), ${id})`
);
return res.json({
code: 200,
message: "todo bien... todo correcto y yo que me alegro",
password,
});
} else {
return res.json({
code: 400,
message: "Bro hiciste algo mal",
});
}
}
That's the right code, I was trying to insert only a id in a table where have 2 foreign keys, and the problem with the 36 is that I was getting my id like a json type.
Upvotes: 0
Reputation: 64
I think your problem is the lack of brackets after values. Values is called like
VALUES(${id})
Where you have:
VALUES ${id}
Upvotes: 1