草庐IT

javascript - 为什么我在进行突变时会收到 "Cannot return null for non-nullable field"错误?

coder 2025-02-26 原文

<分区>

我正在尝试服务器端的 (Apollo) GraphQL,但遇到了一个可能很愚蠢的问题。我正在尝试注册用户,但不断收到下面链接图片中显示的错误。问题是什么?忽略非常简单的身份验证流程,因为我只是在测试 GraphQl

以下是相关的代码片段:

架构

export default `

type User {
    id: ID!
    name: String!
    email: String!
}

type Query {
    allUsers: [User]
  currentUser: User
}

type Mutation {
    createAccount(name: String!, email: String!, password: String!): User
    loginUser(email: String!, password: String!): User
    updatePassword(email: String!, password: String!, newPassword: String!): User
    deleteAccount(email: String!, password: String!): User
}

`

解析器

createAccount: async (
  parent,
  { name, email, password },
  { User },
  info
) => {
  try {
    // Check for invalid (undefined) credentials
    if (!name || !email || !password) {
      return 'Please provide valid credentials';
    }

    // Check if there is a user with the same email
    const foundUser = await User.findOne({ email });

    if (foundUser) {
      return 'Email is already in use';
    }

    // If no user with email create a new user
    const hashedPassword = await bcrypt.hash(password, 10);
    await User.insert({ name, email, password: hashedPassword });

    const savedUser = await User.findOne({ email });

    return savedUser;
  } catch (error) {
    return error.message;
  }
},

有关javascript - 为什么我在进行突变时会收到 "Cannot return null for non-nullable field"错误?的更多相关文章

随机推荐