hasMany sequelize关联中的错误[DEP0097] DeprecationWarning

时间:2020-04-07 03:24:32

标签: node.js sequelize.js

非常感谢您。 我正在尝试执行以下关联用户1-> N联系人,关联,但连续HasMany中的关联不起作用: 我没有找到任何解决此问题的有效方法,如果我从findAll方法中删除了include,则代码可以正常工作 **src/app/models/User.js**

import Sequelize, { Model } from 'sequelize';
import bcrypt from 'bcryptjs';

class User extends Model {
  static init(sequelize) {
    super.init(
      {
        name: Sequelize.STRING,
        phone: Sequelize.STRING,
        email: Sequelize.STRING,
        password: Sequelize.VIRTUAL,
        password_hash: Sequelize.STRING,
      },
      {
        sequelize,
      }
    );
    this.addHook('beforeSave', async user => {
      if (user.password) {
        user.password_hash = await bcrypt.hash(user.password, 8);
      }
    });
    this.addHook('beforeCreate', async user => {
      user.name = user.name.toLowerCase();
      user.phone = user.phone.toLowerCase();
      user.email = user.email.toLowerCase();
    });
    return this;
  }

  checkPassword(password) {
    return bcrypt.compare(password, this.password_hash);
  }

  static associate(models) {
    this.hasMany(models.Contact);
  }
}
export default User;

**src/app/models/User.js**

import Sequelize, { Model } from 'sequelize';

class Contact extends Model {
  static init(sequelize) {
    super.init(
      {
        name: Sequelize.STRING,
        lastname: Sequelize.STRING,
        phone: Sequelize.STRING,
        email: Sequelize.STRING,
        fk_user_id: Sequelize.INTEGER,
      },
      {
        sequelize,
      }
    );
    this.addHook('beforeCreate', async contact => {
      contact.name = contact.name.toLowerCase();
      contact.lastname = contact.lastname.toLowerCase();
      contact.phone = contact.phone.toLowerCase();
      contact.email = contact.email.toLowerCase();
    });
    return this;
  }

  static associate(models) {
    this.hasOne(models.User, { foreignKey: 'fk_user_id' });
  }
}
export default Contact;

跑步时 src/app/controllers/UserController.js

  async index(req, res) {
     const contactList = await User.findAll({
       include: [
         {
           model: Contact,
         },
       ],
     });

    return res.json(contactList);
  }

我收到以下错误 (node:20116) [DEP0097] DeprecationWarning: Using a domain property in MakeCallback is deprecated. Use the async_context variant of MakeCallback or the AsyncResource class instead 但是删除包含代码时,代码可以正常工作。 仓库 https://github.com/Andersonfrfilho/Populus.git

0 个答案:

没有答案