【发布时间】:2018-10-05 21:28:40
【问题描述】:
我正在尝试在服务器端使用 (Apollo) GraphQL,并且遇到了一个可能很愚蠢的问题。我正在尝试注册用户,但不断收到下面链接图片中显示的错误。问题是什么?忽略非常简单的身份验证流程,因为我只是在测试 GraphQl
这里是相关代码sn-ps:
架构
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 node.js express graphql apollo-server