密码不Graphql和节点定义
我试图创建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(bcryptpare(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(bcryptpare(password, args.password));
也许你得到的错误,因为password
没有在代码中定义。你可能想要做user.password
?
密码不Graphql和节点定义
我试图创建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(bcryptpare(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(bcryptpare(password, args.password));
也许你得到的错误,因为password
没有在代码中定义。你可能想要做user.password
?