I'm trying to develop an API post, in middle execution I have validation such as check name already in use or not. I set error handler callback, it successfully send response 'Already registered', but when I checked to CLI, it show error
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
I dont know whats wrong, I use this error handler in the past and it seems look ok.
Here is my code in router:
createUserAccount: async function (req, res) {
const programData = req.body;
try {
await service.create(programData, function (code, err, result) {
if (err) {
if(code === 409){
res.status(HTTPSTATUS.CONFLICT).send(err.message);
} else {
res.status(HTTPSTATUS.BAD_REQUEST).send(err.message);
}
} else {
res.status(HTTPSTATUS.CREATED).json(result);
}
})
} catch (e) {
console.log(e)
res.status(HTTPSTATUS.BAD_REQUEST).json("Failed.");
}
Here is my function in my service:
const config = require('#configs/config.json')
const sequelize = require('sequelize');
const SEQUELIZE = new sequelize(config[env]);
module.exports = {
createAccount: async (name, password, callback) => {
try {
let check,
institution_id;
const checkName = await Profile.count(
{
where: {
name: name
}
}
);
//result checkName = 1
if(checkName > 0){
//then successfully execute this condition and
return callback(409, 'Already registered.', null);
//this show in console ----> POST /API/v1/user/profile 409 616.152 ms - 31
}
await Login.create({
username: email,
password: password
}).then(resLogin => {
const response = {
id: resLogin.id,
}
callback(201, null, response);
}).catch( error => {
callback(400, error, null);
})
} catch (e) {
callback(400, e, null);
}
},
create: async (payload, callback) => {
let loginID = null;
let {
profile,
address
} = payload;
let {
name,
email,
password
} = profile;
try {
await module.exports.createAccount(name, password, function (code, error, result) {
if(error){
const res = {message: error};
//what I need is the execution is end in here
return callback(code, res, null);
}
loginID = result.id;
});
//but the fact is it still execute this whole function if got callback error from createAccount()
let transaction = await SEQUELIZE.transaction();
await Address.create(address, {transaction})
.then( async resAddress => {
await transaction.commit();
return callback(201, null, resProfile);
}).catch(async e => {
return callback(400, e, null);
})
} catch (e) {
console.log(e);
callback(e, null);
}
};
Related
I have a React application with a Node.js backend.
I have this route on the backend:
router.put(
'/testimonial/:testimonialId/:action',
[authenticate, validateUser],
async (req, res) => {
if (req.params.action === 'ACCEPT') {
const { err, status, data } =
await notificationController.acceptTestimonial(
req.params.testimonialId,
req.user._id
);
res.status(status).send({ err, data });
} else if (req.params.action === 'REJECT') {
const { err, status, data } =
await notificationController.rejectTestimonial(
req.params.testimonialId,
req.user
);
res.status(status).send({ err, data });
}
}
);
These are the corresponding Controllers: (acceptTestimonial/rejectTestimonial)
const acceptTestimonial = async (testimonialId, userId) => {
try {
await Users.findOne({ _id: userId }, async function (err, creator) {
if (err) return { err, status: 404, data: null };
else {
const updatedTestimonials = creator.testimonials.map((testimonial) => {
if (testimonial._id == testimonialId) {
testimonial.status = 'PUBLISHED';
}
return testimonial;
});
creator.testimonials = updatedTestimonials;
await creator.save();
return { err: null, status: 200, data: creator };
}
});
} catch (err) {
return { err: err.toString(), status: 500, data: null };
}
};
const rejectTestimonial = async (testimonialId, userId) => {
try {
await Users.findOne({ _id: userId }, async function (err, creator) {
if (!creator)
return { err: 'Creator not found!', status: 404, data: null };
else {
const updatedTestimonials = creator.testimonials.map((testimonial) => {
if (testimonial._id === testimonialId) {
testimonial.status = 'REJECTED';
}
return testimonial;
});
creator.testimonials = updatedTestimonials;
await creator.save();
return { err: null, status: 200, data: creator };
}
});
} catch (err) {
return { err: err.toString(), status: 500, data: null };
}
};
When this route runs and performs the operations, I get this error in the console
UnhandledPromiseRejectionWarning: TypeError: Cannot destructure property 'err' of '(intermediate value)' as it is undefined.
I'm unable to figure out what the problem is, is there a problem with destructuring or passing values ?
Thanks in advance.
it is because, you aren't "returning" anything in try block of your controllers. The return statement is actually of callback function.
await is used to replace callback & promise.then so you have to return something in try block. You can achieve this like:
const rejectTestimonial = async (testimonialId, userId) => {
try {
// await Users.findOne({ _id: userId }, async function (err, creator) {
// if (!creator)
// return { err: 'Creator not found!', status: 404, data: null };
// else {
// const updatedTestimonials = creator.testimonials.map((testimonial) => {
// if (testimonial._id === testimonialId) {
// testimonial.status = 'REJECTED';
// }
// return testimonial;
// });
// creator.testimonials = updatedTestimonials;
// await creator.save();
// return { err: null, status: 200, data: creator };
// }
// });
// 👆 instead of this
// 👇 do this
const creator = await Users.findOne({ _id: userId }); // for "_id", you can also simply do: await User.findById(userId);
if (!creator)
return { err: 'Creator not found!', status: 404, data: null };
// creator.testimonials.forEach((testimonial) => {
// if (testimonial._id === testimonialId)
// testimonial.status = 'REJECTED';
// });
const updatedTestimonials = creator.testimonials.map((testimonial) => {
if (testimonial._id === testimonialId)
testimonial.status = 'REJECTED';
return testimonial;
});
creator.testimonials = updatedTestimonials;
await creator.save();
return { err: null, status: 200, data: creator };
} catch (err) {
return { err: err.toString(), status: 500, data: null };
}
}
Here your controller is returning something in both try & catch cases!
I have this function
const deleteMsg = async (userId, messagesWith, messageId) => {
try {
const user = await Chat.findOne({ user: userId });
const chat = user.chats.find(
chat => chat.messagesWith.toString() === messagesWith,
);
if (!chat) {
return;
}
const messageToDelete = chat.messages.find(
message => message._id.toString() === messageId,
);
if (!messageToDelete) return;
if (messageToDelete.sender.toString() !== userId) {
return;
}
const indexOf = chat.messages
.map(m => m._id.toString())
.indexOf(messageToDelete._id.toString());
await chat.messages.splice(indexOf, 1);
await user.save();
return { success: true };
} catch (error) {
console.error(error);
return { success: false };
}
};
and calling it
const { success } = await deleteMsg(userId, messagesWith, messageId);
getting this error though
Cannot destructure property 'success' of '(intermediate value)' as it is undefined.
I am trying to do some operations using oncall function where,
I am first checking if user exists in database, then if he exists then do some operations like updating the DB.
well the function is working as expected and updating the DB as well, but its returning response as null so I can't tell what exactly happening.
exports.populateCart = functions.https.onCall((data, context) => {
const user = context.auth.uid;
DB.collection('users').doc(user).get().then((usr_resp) => {
if (usr_resp.exists) {
DB.collection('products').doc(data.productKey).get().then((prod_res) => {
DB.collection('carts').add({
//some data
}).then(() => {
return {
response: 'CART UPDATED'
};
}).catch((carts_err) => {
return {
response: 'ERROR WHILE POPULATING CART : ' + carts_err
}
});
}).catch((prod_err) => {
return {
response: 'ERROR WHILE FETCHING PRODUCT : ' + prod_err
};
});
}
else {
return {
response: 'USER DOES NOT EXISTS OR USER NOT AUTHENTICATED'
};
}
}).catch((usr_err) => {
return {
response: 'ERROR WHILE FETCHING USER : ' + usr_err
};
});
});
There is one more similar question on stack overflow, where the solution is proposed to use,
reject(err);
resolve(lists);
methods, Please if the solution is using above methods, then please explain the use cases of this method in detail and if there are more methods like this please explain them as well.
Firebase function not returning data
You need to return every promise that you are running:
exports.populateCart = functions.https.onCall((data, context) => {
const user = context.auth.uid;
return DB.collection('users').doc(user).get().then((usr_resp) => {
if (usr_resp.exists) {
return DB.collection('products').doc(data.productKey).get().then((prod_res) => {
return DB.collection('carts').add({
//some data
}).then(() => {
return {
response: 'CART UPDATED'
};
}).catch((carts_err) => {
return {
response: 'ERROR WHILE POPULATING CART : ' + carts_err
}
});
}).catch((prod_err) => {
return {
response: 'ERROR WHILE FETCHING PRODUCT : ' + prod_err
};
});
}
else {
return {
response: 'USER DOES NOT EXISTS OR USER NOT AUTHENTICATED'
};
}
}).catch((usr_err) => {
return {
response: 'ERROR WHILE FETCHING USER : ' + usr_err
};
});
});
Try copying the above code. Also using async functions will make it look better like this:
exports.populateCart = functions.https.onCall(async (data, context) => {
try {
const usr_resp = await DB.collection('users').doc(user).get()
if (!usr_resp.exists) throw new Error("USER DOES NOT EXISTS OR USER NOT AUTHENTICATED")
const prod_res = await DB.collection('products').doc(data.productKey).get()
await DB.collection('carts').add({ . someData . })
return { response: "Cart Updated" }
} catch (e) {
console.log(e)
return { response: e }
}
});
You are not returning your promise. (see line 3 below)
exports.populateCart = functions.https.onCall((data, context) => {
const user = context.auth.uid;
// add return here before Db.collection()
return DB.collection('users').doc(user).get().then((usr_resp) => {
if (usr_resp.exists) {
return DB.collection('products').doc(data.productKey).get().then((prod_res) => {
return DB.collection('carts').add({
//some data
}).then(() => {
return {
response: 'CART UPDATED'
};
}).catch((carts_err) => {
return {
response: 'ERROR WHILE POPULATING CART : ' + carts_err
}
});
}).catch((prod_err) => {
return {
response: 'ERROR WHILE FETCHING PRODUCT : ' + prod_err
};
});
}
else {
return {
response: 'USER DOES NOT EXISTS OR USER NOT AUTHENTICATED'
};
}
}).catch((usr_err) => {
return {
response: 'ERROR WHILE FETCHING USER : ' + usr_err
};
});
});
If my controller makes multiple db queries in the same async function should each db query be wrapped in it's own individual try/catch block or is it fine to have all db queries in the same try/catch? What is the reasoning for either option?
All db queries in their own try/catch example:
const confirmEmailVerification = async (req, res) => {
const { token } = req.body;
let user;
try {
const result = await db.query(
'SELECT user_account_id FROM user_account WHERE email_verification_token = $1',
[token]
);
if (result.rows.length === 0) {
return res
.status(400)
.json('Please verify your account by clicking the link in your email');
}
user = result.rows[0].user_account_id;
} catch (err) {
console.error(err.message);
return res.status(500).json('Server Error');
}
try {
const active = await db.query(
'UPDATE user_account SET email_verified = TRUE WHERE user_account_id = $1',
[user]
);
return res.status(200).json({
message: 'Email has been verified, Please login',
});
} catch (err) {
console.error(err.message);
return res.status(500).json('Server Error');
}
};
All db queries in the same try/catch example:
const confirmEmailVerification = async (req, res) => {
const { token } = req.body;
let user;
try {
const result = await db.query(
'SELECT user_account_id FROM user_account WHERE email_verification_token = $1',
[token]
);
if (result.rows.length === 0) {
return res
.status(400)
.json('Please verify your account by clicking the link in your email');
}
user = result.rows[0].user_account_id;
const active = await db.query(
'UPDATE user_account SET email_verified = TRUE WHERE user_account_id = $1',
[user]
);
return res.status(200).json({
message: 'Email has been verified, Please login',
});
} catch (err) {
console.error(err.message);
return res.status(500).json('Server Error');
}
};
This depends on that, if you want the sequence of functions to continue after previous function throws error.
In your case it is useless, because in either errors you finish with res.status(500).json('Server Error').
But sometimes you want to continue, even if some of the functions in the chain throws error, eg.:
let errors = []
try {
f1()
} catch (e) {
errors.push(e)
}
try {
f2()
} catch (e) {
errors.push(e)
}
try {
f3()
} catch (e) {
errors.push(e)
}
If you put this is in one try/catch block, you would stop on the error of f1() and f2() and f3() would not be ran at all.
try {
f1()
f2()
f3()
} catch (e) {
something...
}
I am trying to write an async lambda function which is calling a function for sign up a user in cognito.
my problem is that in my lambda function, it is not waiting for the result and finish the execution. would you mind check what is my issue? I am new to rxjs. please help me.
mylambda function
exports.handler = async (event, context) => {
//poolData and params will fetch from event
let source = await signup(poolData, params);
console.log(source);
});
my signup function
function signup(poolData, body) {
const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
const { username, password, attributes } = body;
const attributesList = [];
if (Array.isArray(attributes)) {
attributesList.push(
...attributes.map(item => new AmazonCognitoIdentity.CognitoUserAttribute(item))
);
}
let source = Observable.create(observer => {
let output = (err, res) => {
if (err)
{
observer.error(err);
}
else
{
const cognitoUser = res.user;
const data = {
username: cognitoUser.getUsername(),
};
observer.next(data);
}
observer.complete();
}
userPool.signUp(username, password, attributesList, null, output);
});
let respond;
let subscriber = {
next(value) {
console.log('Subscriber - next: ', value);
respond = {
'statusCode': 200,
'body': JSON.stringify({
"username": value.username,
})
}
}, error(err) {
console.log('Subscriber - err: ', err);
respond = err;
},
complete() {
console.log('Subscriber - complete');
return response;
}
};
source.subscribe(subscriber);
}
module.exports = signup;
This behavior is totally normal.
So first thing first, an observable is not a promise which means you are not able to await a response with the await keyword, also I don't see anything to be returned from the signup function, which will probably lead to undefined to be logged anyways.
So how to fix that, one way to fix this issue is to use toPromise() which will turn your observable into a promise which then can be awaited wherever needed.
The other way (which is the rxjs way) will be to return from the signup function the observable and inside your handler function to subscribe for the response.
let subscriber = {
next(value) {
console.log('Subscriber - next: ', value);
respond = {
'statusCode': 200,
'body': JSON.stringify({
"username": value.username,
})
}
}, error(err) {
console.log('Subscriber - err: ', err);
respond = err;
},
complete() {
console.log('Subscriber - complete');
return response;
}
};
exports.handler = (event, context) => {
//poolData and params will fetch from event
signup(poolData, params).subscribe(subscriber);
})
I am creating a results object to return a boolean and a string. it is not being reassigned within the if statement. The rest of the code is working correctly and the password gets updated or the right error message is output to the console
i've tried leaving result undefined before the try catch.
async function passwordUpdate(password, currentPassword, newPwd, email) {
let hashedPassword = await bcrypt.hash(newPwd, 10);
let result = { success: false , message: ' '};
try {
bcrypt.compare(currentPassword, password, async function (err, res) {
if (res) {
let updateResult = await updatePwd(hashedPassword, email);
if (updateResult) {
result = { success: true , message: 'Password was updated successfully.'};
}
else {
logger.info('Password was not updated successfully.');
}
} else {
logger.error('Passwords do not match');
result= { success: false , message: 'Your current password was entered incorrectly'};
logger.error(result.message);
}
});
} catch (error) {
result= { success: false , message: 'Failed to compare passwords'}
}
logger.error('result ', result.message);
return result;
}
code is being called by this method
app.post('/passwordUpdate', async (req, res) => {
let pwd = req.body.password;
let cpwd = req.body.currentPwd;
let newPwd = req.body.newPwd;
let email = req.body.email;
try {
let result = await usersModel.passwordUpdate(pwd, cpwd, newPwd, email);
console.log(result, result.success, result.message);
if (result.success) {
res.status(200).json({error: result.message});
}
else {
res.status(404).json({error: result.message});
}
} catch (error) {
console.log(error);
}
});
logger.error(result.message); this line within the else statement is outputting the message as expected but
logger.error('result ', result.message); after the try/catch is outputting a blank message for result.message
When you're doing this:
let result = await usersModel.passwordUpdate(pwd, cpwd, newPwd, email);
The passwordUpdate function is resolving the promise based on what is in that function's "top level". This means that the callback function of bcrypt.compare doesn't affect the return of passwordUpdate which is why you're not seeing the result you're looking for.
What you can do is wrap the entire thing in a promise and call the resolve/reject inside the bcrypt.compare function.
async function passwordUpdate(password, currentPassword, newPwd, email) {
return new Promise(async function(resolve, reject) {
let hashedPassword = await bcrypt.hash(newPwd, 10)
try {
bcrypt.compare(currentPassword, password, async function(err, res) {
if (res) {
let updateResult = await updatePwd(hashedPassword, email)
if (updateResult) {
resolve({
success: true,
message: 'Password was updated successfully.',
})
} else {
reject({
success: false,
message: 'Password was not updated successfully.',
})
}
} else {
reject({
success: false,
message: 'Your current password was entered incorrectly',
})
}
})
} catch (error) {
reject({ success: false, message: 'Failed to compare passwords' })
}
})
}
You'll also notice I removed the logging, you can put these back if you wish but since this is a promise you can log your errors more centrally from the caller in the .then and .catch or try/catch if you're using async/await.
Also may I suggest that since you'll be able to determine whether it's an error or not based on the resolve and reject, that you can remove the success from the result and only return a string, it'll make the code cleaner:
async function passwordUpdate(password, currentPassword, newPwd, email) {
return new Promise(async function(resolve, reject) {
const hashedPassword = await bcrypt.hash(newPwd, 10)
try {
bcrypt.compare(currentPassword, password, async function(err, res) {
if (res) {
const updateResult = await updatePwd(hashedPassword, email)
if (updateResult) {
resolve('Password was updated successfully.')
} else {
reject('Password was not updated successfully.')
}
} else {
reject('Your current password was entered incorrectly')
}
})
} catch (error) {
reject('Failed to compare passwords')
}
})
}
PS: I didn't test the code pasted above, I only modified your code to better explain it.