为什么我的代码里没有因为循环引用报错
来源:10-6 小心循环导入

小云嘚咿的飘
2019-11-06
理解了老师关于循环引用的讲解,可是在我的代码里并没有因为循环引用报错?跟我的运行环境或依赖包的版本有关系么?
写回答
5回答
-
不会,肯定还是哪里写的不对吧,循环引用是基础问题,不可能因为环境而有变化
012019-11-08 -
qq_野火燎原_0
2020-03-16
我的代码里也有循环引用,但是没有报错,还正确查出结果了,怎么会这样呢?
这是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 }
112020-03-16 -
撸吉他弹代码
2021-04-24
我知道了 刚刚试了一下 在art.js中没受影响的 在favor.js中 收到了循环引用的影响 因为Favor.
getMyClassicFavors()这个静态方法用到了Art 相对应的 获取我喜欢的期刊的接口‘/myfavor’貌似出现了视频中的错误
00 -
撸吉他弹代码
2021-04-24
我的也没有因为循环引用报错呢 不晓得是不是nodejs升级了后 不会出这种错了?
00 -
可乐小猫
2021-04-01
我的也是。。。
00
相似问题