NodeJS - Cannot set Headers after they are sent to the client - javascript

So I've searched around and found out that to fix the said issue, I have to return after sending a response. But my problem is, even though I have return, I still have the error.
const dbEditCourse = (req, res, db, logger) => {
let {
origCourse, code, description, type
} = req.body;
if (!code || !description || !type) {
res.json({
haveEmpty: true
});
return;
}
db.transaction((trx) => {
db.select('*').from('course_strand').where('code', '=', code)
.then(data => {
if (data[0]) {
//error happens in this block of code
res.json({
isSuccess: false
});
return;
//i also tried return res.json({ isSuccess: false });
}
//wrapping this in 'else' also does not work
return db('course_strand')
.returning('*')
.where('code', '=', origCourse)
.update({ code, description, type })
})
.then(course => {
return db('activity_logs')
.returning('*')
.insert({
date: new Date(),
employee_id: req.session.emp_id,
module: "COURSE / STRAND",
activity: "EDIT"
})
})
.then(activity => {
if (activity[0]) {
res.json({
isSuccess: true
});
return;
} else {
res.json({
isSuccess: false
});
return;
}
})
.then(trx.commit)
.catch(err => {
logger.error(err);
trx.rollback;
res.render('pages/error-500');
});
})
.catch(err => logger.error(err));
}
module.exports = {
dbEditCourse
}
What I'm doing to produce the error is, If the record is existing, it will go into the block of code above. Aside from that specific block of code, I don't encounter the error elsewhere. And the code is working fine even though I have the error.

You cannot break a promise chain with return keyword, all .then statements will be executed (exclude you throw an error in a .then), the res.json has been called many times.
Handler all errors (include your error and system error) in catch block.
In catch block, check the error is throwing by you or not to return the response.
const dbEditCourse = (req, res, db, logger) => {
let {
origCourse, code, description, type
} = req.body;
if (!code || !description || !type) {
res.json({
haveEmpty: true
});
return;
}
// util throw a error
const breakWithMyError = () => {
throw new Error("MY_ERROR");
}
db.transaction((trx) => {
db.select('*').from('course_strand').where('code', '=', code)
.then(data => {
if (data[0]) {
//error happens in this block of code
breakWithMyError();
//i also tried return res.json({ isSuccess: false });
}
//wrapping this in 'else' also does not work
return db('course_strand')
.returning('*')
.where('code', '=', origCourse)
.update({ code, description, type })
})
.then(course => {
return db('activity_logs')
.returning('*')
.insert({
date: new Date(),
employee_id: req.session.emp_id,
module: "COURSE / STRAND",
activity: "EDIT"
})
})
.then(activity => {
// revert logic, we check for error case first
if (!activity[0]) {
breakWithMyError();
}
})
.then(trx.commit)
.then(() => {
// finally you can run to here without any error
res.json({
isSuccess: true
});
})
.catch(err => {
// If you any error, the error comes form `breakWithMyError` or any things.
if (err.message === "MY_ERROR") {
// the error throw by `breakWithMyError`
return res.json({
isSuccess: false
});
}
logger.error(err);
trx.rollback;
// Why you return a html page in failed case? `res.status(500).json({message: "Internal server!"});`
res.render('pages/error-500');
});
})
.catch(err => logger.error(err));
}
module.exports = {
dbEditCourse
}

const dbEditCourse = (req, res, db, logger) => {
let {
origCourse, code, description, type
} = req.body;
if (!(code && description && type)) {
res.json({
haveEmpty: true
});
return;
} else { // Please Try this.
db.transaction((trx) => {
db.select('*').from('course_strand').where('code', '=', code)
.then(data => {
if (data[0]) {
//error happens in this block of code
res.json({
isSuccess: false
});
return;
//i also tried return res.json({ isSuccess: false });
}
//wrapping this in 'else' also does not work
return db('course_strand')
.returning('*')
.where('code', '=', origCourse)
.update({ code, description, type });
})
.then(course => {
return db('activity_logs')
.returning('*')
.insert({
date: new Date(),
employee_id: req.session.emp_id,
module: "COURSE / STRAND",
activity: "EDIT"
});
})
.then(activity => {
if (activity[0]) {
res.json({
isSuccess: true
});
return;
} else {
res.json({
isSuccess: false
});
return;
}
})
.then(trx.commit)
.catch(err => {
logger.error(err);
trx.rollback;
res.render('pages/error-500');
});
})
.catch(err => logger.error(err));
}
};
module.exports = {
dbEditCourse
};

Related

How can I check If a value of a Boolean Variable is false?

I don't know If I'm checking for the value of the boolean correctly
what this code does: the user creates a note for himself, his ID is on the note and it needs to belong to a category name that has to be in the category schema ( where my error happens )
exports.postAddNote = (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
const error = new Error("validation failed, entered data is incorrect");
throw error;
}
const content = req.body.content;
const tags = req.body.tags;
const categoryName = req.body.categoryName;
let creator;
const note = new Note({
content: content,
categoryName: categoryName, // work
tags: tags,
creator: req.userId,
});
Category.find()
.select("-_id")
.select("-__v")
.select("-notesId")
.then((categories) => {
console.log(categories); //stripping everything but names off categories
const CategoryExists = categories.some(
(category) => category.name === categoryName
);
console.log(CategoryExists); // ~~~~~~~~~~ this logs correctly
if (CategoryExists === -0) { // ~~~~~~~~~~ what i want: if the value is false
return res.json({ Error: "The category you entered does not exist" });
}
note // ~~~~~~~~~~ the code stops here :/ it doesn't save the note
.save()
.then((note) => {
console.log("saved note");
User.findById(req.userId);
})
.then((user) => {
creator = user;
user.notes.push(note);
return user.save();
})
.then((result) => {
res.status(201).json({
info: {
dateCreated: new Date().toISOString(),
status: "Note Created Successfully",
creator: { _id: creator._id, email: creator.email },
},
});
})
.catch((err) => {
if (!err.statusCode) {
err.statusCode = 500;
}
});
})
.catch((err) => {
console.log(err);
next();
});
};
if (CategoryExists === -0)
should be
if (CategoryExists === false)
or just
if (!CategoryExists)
i believe. did you try that? not sure why you are using -0. the return value for some() is either going to be true or false.
try this:
if (!CategoryExists) {
return res.json({ Error: 'The category you entered does not exist' });
}

Not returning an error after failed post request - axios, express, node.js

I am trying to implement the validation of password change and the issue I have is that I am not getting errorMessage back from the server in case of an error. I have managed to get it work and send back response after the password was updated. Also, I can console.log the error message on the back end but it's not returning an object with errorMessage to the front end.
if (!currentPassword) {
console.log("no current password");
return res
.status(400)
.json({ errorMessage: "Please confirm your current password" });
}
On the front code looks like this:
handleSubmit = (event) => {
event.preventDefault();
const authorization = localStorage.getItem("accessToken");
axios
.put(
`${process.env.REACT_APP_SERVER_URL}/settings/password`,
this.state.user,
{
headers: {
authorization,
},
}
)
.then((res) => {
if (res.errorMessage) {
console.log(res, "Unsuccessful password updated");
} else {
console.log("updating - res:", res);
this.setState({
user: res.data,
});
}
})
.catch((err) => {
console.log(err, "ERROR");
});
};
Everytime there is an error, I am not consol login the actual erroMessage but it is being catched in catch. What is the cause of that?
Thanks
Not a direct res its available under res.data.
Response schema of axios
use
if (res.data.errorMessage) {
instead of
if (res.errorMessage) {
For better understanding you need to console.log(res). Then you could understand the structure of the response
router.put("/password", isLoggedIn, (req, res, next) => {
const { currentPassword, newPassword, newPasswordConfirm } = req.body;
User.findById(req.user._id)
.then((user) => {
bcrypt.compare(currentPassword, user.password).then((isSamePassword) => {
if (!isSamePassword) {
console.log(
"Incorrect current password. To change your password try again!"
);
return res.status(400).json({
errorMessage:
"Incorrect current password. To change your password try again!",
});
}
return bcrypt
.genSalt(saltRounds)
.then((salt) => bcrypt.hash(newPassword, salt))
.then((hashedPassword) => {
User.findByIdAndUpdate(
req.user._id,
{ password: hashedPassword },
{ new: true }
)
.then((user) => {
console.log("user's password successfully changed");
res.status(200).json(user);
})
.catch((err) => {
res.status(500).json({ errorMessage: err.message });
});
})
.catch((err) => {
res.status(500).json({ errorMessage: err.message });
});
});
})
.catch((err) => {
console.log(err);
res.status(500).json({ errorMessage: err.message });
});
});

Unhandled Rejection (TypeError): Cannot read property 'statusText' of undefined

I am facing this issue while trying to implement a delete functionality using react-redux.
action/profile.js
export const deleteExperience = id => async dispatch => {
try {
const res = await axios.delete(`/api/profile/experience/${id}`);
dispatch({
type: UPDATE_PROFILE,
payload: res.data
});
dispatch(setAlert("Experience Removed", "success"));
} catch (err) {
dispatch({
type: PROFILE_ERROR,
payload: { msg: err.response.statusText, status: err.response.status }
});
}
};
reducers/profile.js
case UPDATE_PROFILE:
return {
...state,
profile: payload,
loading: false
};
case PROFILE_ERROR:
return {
...state,
error: payload,
loading: false
};
The problem is in the deletion API. API should work like this http://localhost:5000/api/profile/experience/5df8f54012b7a81ac04d6b25 but somehow it is working in this way also http://localhost:5000/api/profile/experience/5d with user's token. So I have changed api code from
try {
const profile = await Profile.findOne({ user: req.user.id })
const removeIndex = profile.education.map(item => item.id).indexOf(req.params.edu_id)
profile.education.splice(removeIndex, 1)
await profile.save()
res.json({ msg: 'Education Deleted'})
} catch (err) {
console.log(err.message)
res.status(500).send('Server Error')
}
to
try {
const foundProfile = await Profile.findOne({ user: req.user.id });
const eduIds = foundProfile.education.map(edu => edu._id.toString());
const removeIndex = eduIds.indexOf(req.params.edu_id);
if (removeIndex === -1) {
return res.status(500).json({ msg: "Server error" });
} else {
foundProfile.education.splice(removeIndex,1);
await foundProfile.save();
res.json({ msg: 'Education Deleted'})
}
} catch (error) {
console.error(error);
return res.status(500).json({ msg: "Server error" });
}
Now it is working fine.

Axios.delete is not working the way I want it too

It's a gym app which when a user books themselves into a class, the class saves the userId as a user which will be attending, then also in the user model you also get the classes in which the user is attending too.
Currently hitting 500 (Internal Server Error).
These are the axios calls:
deleteClassHandler = () => {
this.deleteUserClassHandler();
const data = {
userId: this.props.userId,
classId: this.props.id
}
axios.delete('/api/classes/remove', data)
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
}
deleteUserClassHandler = () => {
const data = {
userId: this.props.userId,
classId: this.props.id
}
axios.delete('/api/auth/remove', data)
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
}
The this.props.userID and this.props.id are populated fine with the right values.
These are the routes -
Classes routes:
router.delete('/remove', ClassesController.deleteUser);
Auth routes:
router.delete('/remove', UserController.deleteClass);
This are the controllers:
Classes controller -
exports.deleteUser = (req, res) => {
console.log('cl userid ', req.body.userId);
console.log('cl classid ', req.body.classId);
GymClass.findById({
_id: req.body.classId
}, 'classMembers', (err) => {
if (err) {
console.log('class up here');
res.status(401).json({
message: "Error Occured!"
})
} else {
GymClass.findByIdAndDelete({
"classMembers.userId" : mongoose.Types.ObjectId(req.body.userId)
}, (err) => {
if(err) {
console.log('class down');
res.status(401).json({
message: "Error Occured!"
})
} else {
res.status(200).json({
message: "Success!"
})
}
});
}
})
}
Auth controller -
exports.deleteClass = (req, res) => {
console.log('auth userid', req.body.userId);
console.log('auth classid', req.body.classId);
User.findById({
_id: req.body.userId
}, 'bookedClasses', (err) => {
if (err) {
console.log('auth up here');
res.status(401).json({
message: "Error Occured!"
})
} else {
GymClass.findByIdAndDelete({
"bookedClasses.classId" : mongoose.Types.ObjectId(req.body.classId)
}, (err) => {
if(err) {
console.log('auth down here');
res.status(401).json({
message: "Error Occured!"
})
} else {
res.status(200).json({
message: "Success!"
})
}
});
}
})
}
I am by no means a backend superstar and I have hit a brick wall with this one, does anyone here know how I could maybe possibly change the code and the way I am tackling this? Any issues spotted? I have got a 500 server error and I am not sure what to do. I can always post the two models for the user and classes if needed.
This was also something I tried but did not work -
axios.delete('/api/classes/remove', {
data: {
userId: this.props.userId,
classId: this.props.id
}
})
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
}

Mongoose promise in foreach failed in my case

How to do promise with forEach? I want to get all jobs, but get the data of applicants. My Job schema already have the applicant id, but how to query the user and merge their detail in the output?
Job.find({}).then(result => {
result.forEach(obj =>{
const applicant_id = obj.applicant._id
if(applicant_id){
User.findOne({_id: applicant_id})
.then(user=>{
return res.json({
status: 1,
data: {
...obj,
applicant: {
...user
}
}
})
})
}
})
}).catch(err => {
if(err){
return res.status(400).send({
msg: err
})
}
})
I tried Promise but I'm stuck merging user into the Job obj,
Job.find({}).then(result => {
let promiseArray = []
result.forEach(obj =>{
const applicant_id = obj.applicant._id
if(applicant_id){
promiseArray.push(
User.findOne({_id: applicant_id}))
}
})
return Promise.all(promiseArray)
}).then(user => {
console.log(user)
//this work but this is only the user's data,
//I need it to be within obj which is Job data
})
You first need to filter items in result to exclude those without applicant id, then map this array to array of promises, and finally pass it to Promise.all. This should do it:
Job.find({}).then(result => {
const promises = result
.filter(obj => obj.applicant._id)
.map(obj => {
const applicant_id = obj.applicant._id
return User.findOne({ _id: applicant_id })
.then(user => {
return res.json({
status: 1,
data: {
...obj,
applicant: {
...user
}
}
})
})
})
return Promise.all(promises)
}).catch(err => {
if (err) {
return res.status(400).send({
msg: err
})
}
})
Here's a tested and working solution:
Job.find({ applicant: { $ne: null } }).populate('applicant').then(result => {
res.send(result);
}).catch(err => {
return res.status(400).send({
msg: err
})
});

Categories