Node JS,我怎么能使用Squlize.create corectly

时间:2019-04-17 21:13:33

标签: node.js sequelize.js

我正从1天前开始尝试解决此问题。为什么我的异步功能不等待结果?数据已正确插入数据库中,但graphql在查询完成之前未获得结果。

const resolvers = {
    register: async (
        parent,
        { contact_name, username, password },
        { models }
    ) => {
        const user = {
            contact_name,
            username,
            password
        };
        const registeredUser = await models.Member.create(user)

        try {
            if (typeof registeredUser.member_key === 'number') {
                return true;
            } else {
                return false;
            }
        } catch (error) {
            throw new Error(error);
        }
    }
    Member: {
        config: (parent, args, { models }) => {
            return models.Config.findAll({
                where: {
                    member_key: parent.member_key
                }
            });
        }
    }
}

如果我console.log之后const registeredUser = await models.Member.create(user),我得到以下信息:

member {
    dataValues:
       { member_key: null,
         contact_name: 'Alex',
         username: 'tornado',
         password:
          '$2b$12$6MKWx.LUXXXaIHHIFUp1DuOMkMNLc76HcvTN6ZayLv.a7iG70O3JK' },
      _previousDataValues:
       { contact_name: 'Alex',
         username: 'tornado',
         password:
          '$2b$12$6MKWx.LUXXXaIHHIFUp1DuOMkMNLc76HcvTN6ZayLv.a7iG70O3JK',
         member_key: null,
         member_type: undefined,
         email_address: undefined },
      _changed:
       { contact_name: false,
         username: false,
         password: false,
         member_key: false,
         member_type: false,
         email_address: false },
      _modelOptions:
       { timestamps: false,
         validate: {},
         freezeTableName: true,
         underscored: false,
         paranoid: false,
         rejectOnEmpty: false,
         whereCollection: null,
         schema: null,
         schemaDelimiter: '',
         defaultScope: {},
         scopes: {},
         indexes: [],
         name: { plural: 'members', singular: 'member' },
         omitNull: false,
         sequelize:
          Sequelize {
            options: [Object],
            config: [Object],
            dialect: [MysqlDialect],
            queryInterface: [QueryInterface],
            models: [Object],
            modelManager: [ModelManager],
            connectionManager: [ConnectionManager],
            importCache: [Object],
            test: [Object] },
         hooks: { beforeCreate: [Array] } },
      _options:
       { isNewRecord: true,
         _schema: null,
         _schemaDelimiter: '',
         attributes: undefined,
         include: undefined,
         raw: undefined,
         silent: undefined },
      isNewRecord: false,
      null: 34490 }

,并且在代码的最后一行中指定了member_key,但未在dataValues中进行更新,我确定确实做错了什么,但我不知道该怎么做。 BTW member_key是通常的ID,只有自定义才能反映我的旧数据库:)

这个问题是graphql查询也给了我一个错误,因为它在我可以从数据库取回结果之前就已经完成了,我认为是因为await在这行上不起作用:

constregisteredUser =等待模型。会员。创建(用户)

1 个答案:

答案 0 :(得分:1)

我通过返回令牌来解决了这个问题。因为无论如何我都需要在前端使用令牌,所以我在创建令牌时添加了member_key,username和contact_name,因此我将它们包含在令牌中。

这是寄存器突变的解决方法,其他仍然存在类似的问题

register: async (
    parent,
    { contact_name, username, password },
    { models, secret }
) => {
    const user = {
        contact_name,
        username,
        password
    };
    await models.Member.create(user);
    const member = await models.Member.findOne({
        where: { username }
    });

    if (!member) {
        throw new Error('There is a problem with the registration');
    }
    return {
        token: createToken(member, secret, '30m')
    };
},

还将缺少的字段添加到令牌创建中,它们是member_key和联系人姓名,这是修改后的符号令牌

const createToken = (user, secret, expiresIn) => {
    const { member_key, contact_name, username } = user;
    return jwt.sign({ member_key, contact_name, username }, secret, {
        expiresIn
    });
};