In Node.js, I created an API to check if a customer exists based on their mobile number or email. However, when I pass values that aren’t present in the database, it still shows a “success” response. How can I write the logic to return an appropriate message if the customer is not found?
Node js – model file code —
export async function existing_customer_check(data){
const email = data.email;
const mobile = data.mobile;
const register_customer_check_query = `SELECT email , mobile FROM users WHERE email = ? || mobile = ? `;
try{
const result = await new Promise((resolve,reject)=>{
pool.query(register_customer_check_query,[email,mobile],(error,result,fields)=>{
if(error){
reject(error);
}
else{
resolve(result);
}
})
})
return result;
}catch(error){
throw new Error(error.message);
}
}
controller file
export async function existing_customer_check(data){
const email = data.email;
const mobile = data.mobile;
const register_customer_check_query = `SELECT email , mobile FROM users WHERE email = ? || mobile = ? `;
try{
const result = await new Promise((resolve,reject)=>{
pool.query(register_customer_check_query,[email,mobile],(error,result,fields)=>{
if(error){
reject(error);
}
else{
resolve(result);
}
})
})
return result;
}catch(error){
throw new Error(error.message);
}
}