.../routers/posts.js:65
allPosts.sort(sortDateDescending);
TypeError: allPosts.sort is not a function

이유가 뭐지?? allPosts가 array가 아닌가?? -> console 로 출력 해보자

 

<시도>

const allPosts = Post.findAll();
console.log(allPosts);
console.log(Array.isArray(allPosts);

 

<결과>

// console.log(allPosts)의 출력 결과
Promise {
  <pending>,
  [Symbol(async_id_symbol)]: 1230,
  [Symbol(trigger_async_id_symbol)]: 1222
}

// console.log(Array.isArray(allPosts))의 출력 결과
false

TypeError: allPosts.sort is not a function

 

>>  첫번째 출력은 뭔지 모르겠으나, Array 형식이 아니라고 하여 형식을 변환 후 출력

const allPosts = Post.findAll();
const allPostsArray = Array.from(allPosts)

console.log(allPostsArray);  // 출력 : []
console.log(Array.isArray(allPostsArray));// 출력: true

 

<< 코드는 정상적으로 작동하는데 받아오는 값이 없다>>

  1. Sequelize 에서 findAll 조사 : https://sequelize.org/api/v6/class/src/model.js~model#static-method-findAll.

..... 문제는 단순했다...   await 를 쓰지 않은 상태에서  array 형태로 변형 시켜서 allPosts 에 아무런 값이 담기지 않았다.

//const allPosts = Post.findAll(); 
const allPosts = await Post.findAll();
 

+ Recent posts