I wrote a function to create a user profile.
I use the synchronise module to get rid of the callback hell.
When I do this, everything works fine:
var sql = jsonSql.build({
type: 'insert',
table: 'tbl_user',
values: {firstname: data.firstname, lastname: data.lastname, email: data.email, website: data.website, password: data.hashedPassword},
});
try {
var query = sync.await(db.query(sql.query, sql.values, sync.defer()));
} catch (err) {
res.status(500).send(err);
return;
}
When sql finds a user with an already used mail address it sends an error. Perfect.
Then I thought it would be a good idea to separate the code where the user is written into the database, because maybe you want to create a user in the admin-backend and then I could reuse the smaller function.
The smaller function:
function createUserInDB(data) {
var sql = jsonSql.build({
type: 'insert',
table: 'tbl_user',
values: {firstname: data.firstname, lastname: data.lastname, email: data.email, website: data.website, password: data.hashedPassword},
});
return db.query(sql.query, sql.values);
}
Which now gets called from the main function:
try {
var query = sync.await(userService.createUserInDB(data, sync.defer()));
console.dir(query);
} catch(err) {
console.dir(err);
res.status(500).send(err);
return;
}
This does not work.
How can I get the result of the smaller function (as error or query result) in my main function, so I am able to use try / catch?
via EscapeNetscape
No comments:
Post a Comment