首页 文章

不知道为什么我的cursorForObjectInConnection返回null?

提问于
浏览
0

我在回复时出现此错误:

对于不可为空的字段TodoEdge.cursor,不能返回null .

这是我的变异代码:

mutateAndGetPayload: async ({text}, context) => {
      let newTodo = new TodoModel({ text, _creatorUserId: context.user._id });
      await newTodo.save(); 
      return {newTodo}; 
  },
  outputFields: {
    todoEdge: {
      type: GraphQLTodoEdge,
      resolve: async ({newTodo}, args,context) => {
        const todos = await getTodosByUserContext(context.user._id, 'any');
        console.log("cursorForObjectInConnection = ",cursorForObjectInConnection(todos, newTodo)) // this logs null?
        return {
          cursor: cursorForObjectInConnection(todos, newTodo),
          node: newTodo,
        };
      },
    },

todos和newTodo是从mongoose数据库中检索出来的 . 我想我正在关注这个相关的todo-modern sample . 救命?

1 回答

  • 1

    让我们试试以下:

    1)从您的数据库中获取待办事项

    const todos = await Todo.find({});
    

    根据您的Todo架构,您可以为特定用户获取待办事项 . Todo.find({userid:context.user._id});

    2)一旦你得到你的待办事项然后得到光标:

    const cursor = offsetToCursor(todos.length);
    

    这对我有用 . 试试看 .

相关问题