'express js & PostgreSQL Put request inserts null in undefined fields
I make an edit profile request where is possible to edit one or more fields. And when i make put request and want change only one field, every undefined field becomes null. How to pass undefined field? Every not mentioned field in request saves its value
//update profile
app.put("/id/:id", async (req,res) => {
try{
const {id} = req.params;
const {username, email, userdescription, photo_url} = req.body;
const ifEmailExists = await isEmailAvailable(email)
const ifUsernameExists = await isUsernameAvailable(username)
if(ifEmailExists && ifUsernameExists){
const updUser = await pool.query(
"UPDATE users SET (username, email, userdescription, photo_url) = ($1, $2, $3, $4) WHERE id = $5",
[username, email, userdescription, photo_url, id]);
res.json(`User data was updated successfully !`);
} else { if(!ifUsernameExists&& !ifEmailExists){
res.json({"msg": "Username and email are already used !"});}
else {
if(!ifEmailExists)
res.json({"msg": "Email is already used !"});
else { if(!ifUsernameExists){
res.json({"msg": "Username is already used !"});}
}
}
}
}catch(err){
console.error(err.message);
}
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
