【问题标题】:Password is not defined in Graphql & NodeGraphql 和 Node 中未定义密码
【发布时间】:2019-02-12 13:32:49
【问题描述】:

我正在尝试使用 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');

User 类型定义了我们想要存储来自用户的哪些数据。

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});
            }
        }


    })
});

登录功能仍然无法识别密码输入。

【问题讨论】:

    标签: node.js graphql


    【解决方案1】:

    您没有在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

    【讨论】:

    • 已注明更正。当它包含在 AuthType 中时,它仍然不起作用。
    • 您究竟使用什么来生成该代码?因为我之前没用过那些GraphQLObjectType
    • 如何生成? .
    • 您从哪里获得 GraphQLObjectType、GraphQLString..?
    • 来自 graphql。需要之后。常量 { graphQLString } = graphql
    【解决方案2】:

    我没有在解析函数中使用 args,而是使用了 {email, password}。

            const RootQuery = new GraphQLObectType({
                  login: {
                        type: AuthType,
                        args: {
                               password: {
                                     type: GraphQLString
                                },
                              email: {
                                     type: GraphQLString
                                          }
                             },
                  resolve(parent, {email, password }) {
                       return User.findOne({
                            email: email
                       })
                        .then(user => {
                         const isEqual = bcrypt. compare(
                                password, user.password));
                          if (!isEqual) {
                               throw new Error('Password is incorrect!');
                           }
    
                        }).then(result => {
                return {
                  userId: result.id,
                 username: result.username
                };
              }).catch(err => {
                 throw err
              });
           }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-26
      • 1970-01-01
      • 2020-01-12
      • 1970-01-01
      • 2014-11-17
      • 2020-03-16
      • 2019-06-06
      • 2017-05-09
      相关资源
      最近更新 更多