[Error] cyclic dependency detected

Q kim·2020년 10월 4일
1

Hello new Error !

목록 보기
1/2

상황파악

for문안에서 외부 API를 두번 이용하였고. 또 같은 for문 안에서 mongoose를 이용해서 Model.create를 하고 있다.

document를 만들때 외부 API를 두번 이용해야 한다.
처음 받아온 API response에 들어있는 list를 이용해서 다시 API 요청을 하고 있는 상황.


시도해본 것들

첫번째_ 하나의 for안에서 너무 많은 일을 하지 않게 하자. [ 땡~! ]

for문을 두번쓰자. 중첩for문 X 같은 층에서 for문 두번 O

첫번째 for문
처음 요청한 API request의 response data만을 이용해 Model.create();(document를 생성)한다.
두번째 for문
첫번째 create가 끝난뒤. 두번째 API 요청을 한다. 이번에 받아온 response data로 한번더 db에 접근해 첫번째 만들어 놓은 document를 수정한다.

두번째_ DB 설정을 바꿔본다. [ 땡~! ]

github mongoose issues from 'AndrewBarba' https://github.com/Automattic/mongoose/issues/6109#


Might be worth noting, I was using the mongodb+srv:// uri structure with a few options:

mongodb+srv://user:password@host.mongodb.net?readPreference=primaryPreferred&retryWrites=true
Once I removed the uri options the issue went away without any other code changes:

mongodb+srv://user:password@host.mongodb.net



세번째_ mongoose options 설정을 바꿔본다. [ 땡~! ]

github mongoose issues from 'gustvao' https://github.com/Automattic/mongoose/issues/6109#


so I was not being able to run my app anymore, I was getting these cyclic dependency errors... the only work-around that worked for me was to add autoIndex: false into mongoose.connect - but doing so my indexes were not being created anymore even after using ensureIndex() later on the application - it would eventually result in the same cyclic dependency errors..

long story short I managed to fix it by removing all attributes INDEX from inside my models and appending them to the model afterwards, like this:

before

client: { type: ObjectId, ref: 'tsCli', required: true, index: true },

after

client: { type: ObjectId, ref: 'tsCli', required: true },
[...]
Schema.index({ client: 1 });

네번째_ mongoose document에 request, response 객체는 넣을수 없다... [ 정답 ! ]

아무 생각없이 Model.create() 할 때 입력 데이터로 response.data를 넣지 않고 resopnse를 통째로 넣어버렸다. 그래서 오류가 나버렸다. http 통신 관련 데이터가 다 있는 걸 넣어버렸으니 오류가 날수 밖에..

이제는 잘 동작한다.. !

profile
https://medium.com/nodejs-server

0개의 댓글