codestates / codestates/Senna-server
[Error] cannot access 'newPosting' before initialization
- Dominant language
- JavaScript
- Stars
- 0
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
### 어떤 에러인가요?
- cannot access 'newPosting' before initialization
newPosting 초기화 전에 사용해서 발생한 에러
### $ 터미널의 에러 코드를 여기 넣어주세요.
- 코드 수정중에 선언되지 않은 변수를 사용했더니 에러가 발생했다.
```
const updateFunc = async (tags, imgs) => {
let result = {}
if(tags) {
let tagArr = tags.split('#').slice(1).map(tag => tag.replace(',', ''))
console.log('tagArr', tagArr)
result = await Posting.findByIdAndUpdate(postingId, {hashtag: tagArr}, {new:true, upsert: true}).exec()
}
if(imgs) {
result = await Posting.findByIdAndUpdate(postingId, {image: imgs}, {new: true})
let deleteImg = beforeImg.filter(img => !newPosting.image.includes(img))
deleteMany(deleteImg)
}
return result
}
const newPosting = await updateFunc(hashtag, content, place, path)
```
### 에러 핸들링 방법
- newPosting 선언 전에 사용된 곳이 있는지 전체 코드를 살펴보았다.
```
const newPosting = await updateFunc(hashtag, content, place, path)
//이전 이미지 지우기
let deleteImg = beforeImg.filter(img => !newPosting.image.includes(img))
console.log('deleteImg',deleteImg)
deleteMany(deleteImg)
```
### 에러 핸들링을 위해 참고한 레퍼런스 링크
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.