Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

Creating the dots·2021년 9월 2일
1

Error

목록 보기
2/7

다음과 같이 작성했을때 Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.에러가 발생했다.

에러발생한 코드

 post: async (req, res) => {
    getUrlTitle(req.body.url, async (err, title) => {
      const created = await url.create({
        url: req.body.url,
        title: title,
      });
    });
    res.status(201).json(created); //getUrlTitle 함수 밖에 위치해있다.
  },

에러해결

 post: async (req, res) => {
    getUrlTitle(req.body.url, async (err, title) => {
      const created = await url.create({
        url: req.body.url,
        title: title,
      });
      res.status(201).json(created); //getUrlTitle 함수안에 위치해야 한다
    });
  },

await 키워드로 레코드를 만들어서 created에 담아주었는데, 이것을 JSON의 형태로 전달해주어 성공적으로 함수가 실행되었음을 async 함수 내에서 res.status(201).json(created)로 전달해줘야한다.

profile
어제보다 나은 오늘을 만드는 중

0개의 댓글