node.js - Graphql & Node 中未定义密码
问题描述
我正在尝试使用 GraphQL 和节点创建登录功能。我已经注册了工作,但是在查询登录功能时,它说密码没有定义。
AuthType
const AuthType = new GraphQLObjectType({
name: 'Auth',
fields: () => ({
userId: {type: GraphQLString},
username: {type: GraphQLString},
email: {type: GraphQLString},
})
});
这保存了我期待的数据。
const RootQuery = new GraphQLObectType({
login: {
type: AuthType,
args: {
password: {
type: GraphQLString
},
email: {
type: GraphQLString
}
},
resolve(parent, args) {
return User.findOne({
email: args.email
})
.then(user => {
const isEqual = new Promise(bcrypt.compare(password, args.password));
if (!isEqual) {
throw new Error('Password is incorrect!');
}
}).then(result => {
return {
userId: result.id,
username: result.username
};
}).catch(err => {
throw err
});
}
}
});
这是检查数据的逻辑,谢谢。
schema.js
const graphql = require('graphql');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const {GraphQLObjectType,
GraphQLInt,
GraphQLString,
GraphQLSchema,
GraphQLID,
GraphQLList,
GraphQLNonNull } = graphql;
const User = require('../models/user');
const Event = require('../models/event');
用户类型定义了我们想要存储的用户数据。
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {type: GraphQLID},
firstname: {type: GraphQLString},
lastname: {type: GraphQLString},
username: {type: GraphQLString},
email: {type: GraphQLString},
password: {type: GraphQLString},
location: {type: GraphQLString},
about: {type: GraphQLString},
gender: {type: GraphQLString},
yob: {type: GraphQLString}, //Year of Birth;
events: {
type: new GraphQLList(EventType),
resolve(parent, args){
// return _.filter(events, {userId: parent.id});
return Event.find({creator: parent.id});
}
}
})
});
登录功能仍然无法识别密码输入。
解决方案
您没有在 中定义密码字段AuthType
,我想您应该执行以下操作:
const AuthType = new GraphQLObjectType({
name: 'Auth',
fields: () => ({
userId: {type: GraphQLString},
username: {type: GraphQLString},
email: {type: GraphQLString},
password: {type: GraphQLString},
})
});
此外,您在此行中有拼写错误:
const RootQuery = new GraphQLObectType({
它应该GraphQLObjectType
代替GraphQLObectType
此外,在这一行中:
const isEqual = new Promise(bcrypt.compare(password, args.password));
可能您在那里遇到错误,因为password
代码中没有定义。你大概想做什么user.password
?