为什么我的代码里没有因为循环引用报错

来源:10-6 小心循环导入

小云嘚咿的飘

2019-11-06

理解了老师关于循环引用的讲解,可是在我的代码里并没有因为循环引用报错?跟我的运行环境或依赖包的版本有关系么?

写回答

5回答

7七月

2019-11-07

不会,肯定还是哪里写的不对吧,循环引用是基础问题,不可能因为环境而有变化

0
1
小云嘚咿的飘
非常感谢!
2019-11-08
共1条回复

qq_野火燎原_0

2020-03-16

我的代码里也有循环引用,但是没有报错,还正确查出结果了,怎么会这样呢?

//img.mukewang.com/szimg/5e6eb3120905a6d520811108.jpg

//img.mukewang.com/szimg/5e6eb348098f7bc308530268.jpg

这是favor.js

const { DataTypes, Model, Op} = require('sequelize')
const { sequelize } = require('../../core/db')
const { Art} = require('./art')

class Favor extends Model {
   static async like (artId, type, uid) {
    // 1.添加记录
    // 2. classic fav_nums
    // 数据库事务,保持数据的一致性
    // ACID
    const favor = await Favor.findOne({
      where: {
        artId,
        type,
        uid
      }
    })
    if (favor) {
      throw new global.errs.LikeError()
    }

    // 事务
    return sequelize.transaction(async t => {
      await Favor.create({
        artId,
        type,
        uid
      }, { transaction: t })
      const art = await Art.getData(artId, type, false)
      await art.increment('favNums', { by: 1 }, {transaction: t})
    })
  }
  static async dislike (artId, type, uid) {
    const favor = await Favor.findOne({
      where: {
        artId,
        type,
        uid
      }
    })
    if (!favor) {
      throw new global.errs.DislikeError()
    }

    // 事务
    return sequelize.transaction(async t => {
      await favor.destroy({
        force: false,
        transaction: t
      })
      const art = await Art.getData(artId, type, false)
      await art.decrement('favNums', { by: 1 }, {transaction: t})
    })
  }

  static async userLikeIt (artId, type, uid) {
    const favor = await Favor.findOne({
      where: {
        artId,
        type,
        uid
      }
    })

    return favor ? true : false
  }

  static async getMyClassicFavors (uid) {
    const arts = await Favor.findAll({
      uid,
      type: {
        [Op.not]: 400
      }
    })
    if (!arts) {
      throw new global.errs.NotFound()
    }
    return await Art.getList(arts)
  }


}

Favor.init({
  uid: DataTypes.INTEGER,
  artId: DataTypes.INTEGER,
  type: DataTypes.INTEGER
}, {
  sequelize,
  tableName: 'favor'
})

module.exports = {
  Favor
}

这是art.js

const { Op } = require('sequelize')
const { Movie, Music, Sentence } = require('../models/classic')
const { Favor } = require('./favor')

class Art {
  constructor(artId, type) {
    this.artId = artId
    this.type = type
  }

  async getDetail (uid) {
    const id = this.artId
    const type = this.type
    const art = await Art.getData(id, type)
    const likeStatus = await Favor.userLikeIt(id, type, uid)
    if (!art) {
      throw new global.errs.NotFound()
    }
    return {
      ...art.dataValues,
      likeStatus
    }
  }

   static async getList (artInfoList) {
    const artInfoObj = {
      100: [],
      200: [],
      300: []
    }
    for (let artInfo of artInfoList) {
      artInfoObj[artInfo.type].push(artInfo.artId)
    }
    let arts = []
    for (let key in artInfoObj) {
      const ids = artInfoObj[key]
      if (ids.length === 0) {
        continue
      }
      key = parseInt(key)
      arts.push(await Art._getListByType(ids, key))
     }
     arts = arts.flat()
     return arts
  }

  static async _getListByType (ids, type) {
    let arts = []
    const finder = {
      where: {
        id: {
          [Op.in]: ids
        }
      }
    }
    const scope = 'bh'
    type = parseInt(type)
    switch (type) {
      case 100:
        arts = await Movie.scope(scope).findAll(finder)
        break;
      case 200:
        arts = await Music.scope(scope).findAll(finder)
        break;
      case 300:
        arts = await Sentence.scope(scope).findAll(finder)
        break;
      case 400:

        break;
      default:
        break;
    }
    return arts
  }
  static async getData (artId, type, useScope = true) {
    const finder = {
      where: {
        id: artId
      }
    }
    let art
    const scope = useScope ? 'bh' : null
    type = parseInt(type)
    switch (type) {
      case 100:
        art = await Movie.scope(scope).findOne(finder)
        break;
      case 200:
        art = await Music.scope(scope).findOne(finder)
        break;
      case 300:
        art = await Sentence.scope(scope).findOne(finder)
        break;
      case 400:

        break;
      default:
        break;
    }
    return art
  }
}

module.exports = {
  Art
}


1
1
ljt1469
我的也是
2020-03-16
共1条回复

撸吉他弹代码

2021-04-24

我知道了   刚刚试了一下  在art.js中没受影响的   在favor.js中 收到了循环引用的影响  因为Favor.

getMyClassicFavors()这个静态方法用到了Art  相对应的  获取我喜欢的期刊的接口‘/myfavor’貌似出现了视频中的错误


0
0

撸吉他弹代码

2021-04-24

我的也没有因为循环引用报错呢  不晓得是不是nodejs升级了后 不会出这种错了?

0
0

可乐小猫

2021-04-01

我的也是。。。

0
0

Node.js+Koa2+MySQL打造前后端分离精品项目《旧岛》

理解异步编程本质/培养面向对象思维,独立完成Node.js服务端开发

2229 学习 · 879 问题

查看课程