无法在 sequelize 中获取 userID?

can not get userID in sequelize?

这是用户模型

const { Model } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
  class User extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate({ Kyc }) {
      // define association here
      this.hasOne(Kyc, { foreignKey: "userID" });
    }
  }
  User.init(
    {
      uuid: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        allowNull: false,
      },
      firstname: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      lastname: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      email: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      role: {
        type: DataTypes.STRING,
        defaultValue: "user",
        allowNull: false,
        validate: {
          roles(value) {
            const rolesArray = ["user", "admin"];
            if (!rolesArray.includes(value)) {
              throw new Error('plese enter valid role "user or admin"');
            }
          },
        },
      },
      password: {
        type: DataTypes.STRING,
        allowNull: false,
      },
    },
    {
      sequelize,
      modelName: "User",
    }
  );
  return User;
};

这是kyc模型


const { Model } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
  class Kyc extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate({ User }) {
      // define association here
      this.belongsTo(User, { foreignKey: "userID", as: "user" });
    }
  }
  Kyc.init(
    {
      uuid: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        allowNull: false,
      },
      docimg: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      details: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      status: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      userID: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
    },
    {
      sequelize,
      modelName: "Kyc",
    }
  );
  return Kyc;
};

kyc中间件

const verifyKyc = async (req, res, next) => {
  // check that user has posted or not if yes then give error
  const user = await User.findByPk(req.user.id);

  const kyc = await Kyc.findOne({
    userID: req.user.id,
  });
  console.log(user.id);
  console.log(kyc);
  if (user.id === kyc) {
  }
  next();
};

错误

正在执行(默认):SELECT“id”、“uuid”、“firstname”、“lastname”、“email”、“role”、“password”、“createdAt”、“updatedAt”从“用户”作为“用户”,其中“用户”。“id”= 1; (sequelize) 警告:模型属性 (userID) 已传递到模型 Kyc 的查找器方法选项中,但 options.where 对象为空。你忘记使用 options.where 了吗? 正在执行(默认):SELECT "id"、"uuid"、"docimg"、"details"、"status"、"userID"、"createdAt"、"updatedAt" FROM "Kycs" AS "Kyc" LIMIT 1;

1
Kyc {
  dataValues: {
    id: 117,
    uuid: '99461f78-4781-42cc-a01f-b6541fda849d',
    docimg: 'admin.png',
    details: 'KSAPK0550P',
    status: 'pending',
    userID: 1,
    createdAt: 2022-06-04T10:59:21.039Z,
    updatedAt: 2022-06-04T10:59:21.039Z
  
  _previousDataValues: {
    id: 117,
    uuid: '99461f78-4781-42cc-a01f-b6541fda849d',
    docimg: 'admin.png',
    details: 'KSAPK0550P',
    status: 'pending',
    userID: 1,
    createdAt: 2022-06-04T10:59:21.039Z,
    updatedAt: 2022-06-04T10:59:21.039Z
  
 
  isNewRecord: false
}

我想打印 userID 但给我这个错误 谢谢你的帮助

您忘记将条件包装到 where 选项中:

const kyc = await Kyc.findOne({
    where: {
      userID: req.user.id,
    }
  });