I'm learning how to write apps using expressJS and Sequelize. So please help me because I'm only used to use sequencial programming. I have 2 models Todo(id, title) and TodoItem(id, content, complete, todoId). I try to write function for changing TodoItem properties. If I'm changing content or complete value it's no problem to me. I know how to develope this. When on request there is todoId which is diffrent than actual value I want to check if todo with that Id exists. I wrote first method in todo controller which check it, but the problem is that it returns Promise, but I want to get Boolean. It looks like this:
exists(todoId) {
Todo.findByPk(todoId)
.then(todo => {
if (!todo) {
console.log(`[exists] Todo ${todoId} not exists`);
return false;
} else {
console.log(`[exists] Todo ${todo.id} exists`);
return true;
}
})
.then(b => {
console.log(`B = ${b}`)
})
.catch(error => { throw error });
}
It is possible to change from async to sync? I show you what I code in todoItem controller so you will imagine what I wanted to do.
update(req, res) {
return TodoItem
.findOne({
where: {
todoId: req.params.todoId,
id: req.params.todoItemId
}
})
.then(
todoItem => {
if (!todoItem) {
return res.status(404).send({ message: 'TodoItem Not Found' });
}
if (req.body.todoId && todoItem.todoId != req.body.todoId) {
console.log(`Check if Todo ${req.body.todoId} exists`);
console.log(todosController.exists(req.body.todoId));
// Here I want to check if todo(req.body.todoId) not exists and if not, then
// res.status(404).send({ message: 'Todo Not Found' });
}
return todoItem
.update(req.body, { fields: Object.keys(req.body) })
.then(todoItem => res.status(200).send(todoItem))
.catch(error => res.status(400).send(error))
}
)
.catch(error => {
res.status(400).send(error);
})
}
You still can write an async code in a sequential
-like way by using async and await keywords.
async exists(todoId) {
const todo = await Todo.findByPk(todoId)
if (!todo) {
console.log(`[exists] Todo ${todoId} not exists`);
return false;
} else {
console.log(`[exists] Todo ${todo.id} exists`);
return true;
}
}
and update looks like:
async update(req, res) {
try {
const todoItem = await TodoItem
.findOne({
where: {
todoId: req.params.todoId,
id: req.params.todoItemId
}
})
if (!todoItem) {
res.status(404).send({ message: 'TodoItem Not Found' });
return
}
if (req.body.todoId && todoItem.todoId != req.body.todoId) {
console.log(`Check if Todo ${req.body.todoId} exists`);
const todoExists = await todosController.exists(req.body.todoId)
console.log(todoExists);
if (!todoExists) {
// Here I want to check if todo(req.body.todoId) not exists and if not, then
res.status(404).send({ message: 'Todo Not Found' });
return
}
}
const updatedTodoItem = await todoItem
.update(req.body, { fields: Object.keys(req.body) })
res.status(200).send(updatedTodoItem)
} catch(error) {
res.status(400).send(error);
}
}
OK, now my code is simple and works fine. If you have some valuable comments, eg best practise, please do so. In todo controller I have
exists(todoId) {
return Todo.findByPk(todoId)
.then(todo => {
if (todo) {
return true;
} else {
return false;
}
});
}
and in todoItem controller:
update(req, res) {
return TodoItem
.findOne({
where: {
todoId: req.params.todoId,
id: req.params.todoItemId
}
})
.then(
async (todoItem) => {
if (!todoItem) {
return res.status(404).send({ message: 'TodoItem Not Found' });
}
if (req.body.todoId && todoItem.todoId != req.body.todoId) {
if (!await todosController.exists(req.body.todoId))
return res.status(404).send({ message: 'Todo Not Found' });
}
return await todoItem
.update(req.body, { fields: Object.keys(req.body) })
.then(todoItem => res.status(200).send(todoItem))
.catch(error => res.status(400).send(error))
}
)
.catch(error => res.status(400).send(error))
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.