Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何正确使用sequelize和promise_Javascript_Node.js_Express_Promise_Sequelize.js - Fatal编程技术网

Javascript 如何正确使用sequelize和promise

Javascript 如何正确使用sequelize和promise,javascript,node.js,express,promise,sequelize.js,Javascript,Node.js,Express,Promise,Sequelize.js,我正在学习如何使用expressJS和Sequelize编写应用程序。所以请帮助我,因为我只使用顺序编程。 我有两个模型Todoid,title和TodoItemid,content,complete,Todoid。我尝试编写函数来更改TodoItem属性。如果我正在改变内容或完整的价值观,这对我来说没有问题。我知道如何开发这个。当接到请求时,有一个todoId,它与实际值不同,我想检查是否存在具有该Id的todo。我在todo控制器中编写了第一个方法来检查它,但问题是它返回承诺,但我想得到布尔

我正在学习如何使用expressJS和Sequelize编写应用程序。所以请帮助我,因为我只使用顺序编程。 我有两个模型Todoid,title和TodoItemid,content,complete,Todoid。我尝试编写函数来更改TodoItem属性。如果我正在改变内容或完整的价值观,这对我来说没有问题。我知道如何开发这个。当接到请求时,有一个todoId,它与实际值不同,我想检查是否存在具有该Id的todo。我在todo控制器中编写了第一个方法来检查它,但问题是它返回承诺,但我想得到布尔值。看起来是这样的:

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 });
}
是否可以从异步更改为同步? 我将向您展示我在todoItem控制器中编写的代码,以便您可以想象我想要做什么

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);
            })
    }

您仍然可以使用async和await关键字以类似顺序的方式编写异步代码

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;
   }
}
更新如下所示:

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);
     }
}

好的,现在我的代码很简单,运行良好。如果您有一些有价值的意见,例如最佳做法,请这样做。 在todo控制器中,我有

exists(todoId) {
        return Todo.findByPk(todoId)
            .then(todo => {
                if (todo) {
                    return true;
                } else {
                    return false;
                }
            });
    }
在todoItem控制器中:

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))
    }

是的,但是async ExistToDoId{仍然返回一个承诺,所以我不能在我的更新函数中以这种方式使用它。问题是我不知道如何停止处理,因为async是单向的,而todoItem.update不会等待。