我在服务器端尝试使用(Apollo)GraphQL,并且遇到了一个可能很傻的问题。我正在尝试注册用户,但一直收到下方链接图片中显示的错误。问题出在哪里?请忽略非常简单的身份验证流程,因为我只是在测试GraphQl。
以下是相关代码片段:
Schema
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;
}
},
resolve: (source, args, { pgApi }) => {pgApi.userInfo(source.userId)}
而不是resolve: (source, args, { pgApi }) => pgApi.userInfo(source.userId)
- Hoxtygen