I used Sequelize Node , So i want to write Sequelize Validation part in separate service class. When i run this code result set to "undefined",please help me resolve this.
userController.js
let userData = request.body;
let result = await userService.createUser(userData);
console.log(result);
user.service.js
module.exports.createUser = async (userData) => {
let user = {
first_name: userData.first_name,
last_name: userData.last_name,
email: userData.email,
password: userData.password
};
await User.create(user).then(result => {
console.log('success');
return result;
}).catch(error => {
console.log('error');
return 'test';
})
CodePudding user response:
The result code is undefined
because the createUser
function don't return any, you should use await/async properly. Try this:
module.exports.createUser = async (userData) => {
let user = {
first_name: userData.first_name,
last_name: userData.last_name,
email: userData.email,
password: userData.password
};
try {
const result = await User.create(user);
console.log('success');
return result;
} catch (error) {
console.log('error');
return 'test';
}
}