• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    해결됨

await 관련 질문드립니다.

20.06.17 16:36 작성 조회수 114

0

안녕하세요. 강의 잘 듣고있습니다. 

다름이 아니라 강의에 나온 내용대로 실습을 진행하고 있는데,

await 구문에서 에러가 발생하여 문의합니다. 

const express = require('express');
const bcrypt = require('bcrypt');
const passport = require('passport');

const { User } = require('../models');


const router = express.Router();

// POST /auth/join
router.post('/join', (req, res, next) => {
const { email, nick, password } = req.body;
try {
const exUser = await User.find( { where: { email } });
if (exUser) {
req.flash('joinError', '이미 가입된 이메일입니다.');
return res.redirect('/join');
}
const hash = await bcrypt.hash(password, 12);
await User.create({
email,
nick,
password: hash
});
return res.redirect('/');
} catch (error) {
console.log(error);
next(error);
}
});

package.json 버전입니다.

"dependencies": {
"bcrypt": "^5.0.0",
"connect-flash": "^0.1.1",
"cookie-parser": "^1.4.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-session": "^1.17.1",
"morgan": "^1.10.0",
"mysql2": "^2.1.0",
"passport": "^0.4.1",
"passport-kakao": "^1.0.0",
"passport-local": "^1.0.0",
"pug": "^3.0.0",
"sequelize": "^5.21.13"
},

실행하면  아래와 같이 await 구문에 async함수가 아니라고 나오는데요..  bcryt.hash 함수도 마찬가지 입니다. 

어떻게 처리해야 할까요? 

[nodemon] starting `node app.js`

C:\nodejs-workspace\nodejs-book\nodebird\routes\auth.js:14

        const exUser = await User.find( { where: { email } });

                       ^^^^^

SyntaxError: await is only valid in async function

?[90m    at wrapSafe (internal/modules/cjs/loader.js:1054:16)?[39m

?[90m    at Module._compile (internal/modules/cjs/loader.js:1102:27)?[39m

?[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)?[39m

?[90m    at Module.load (internal/modules/cjs/loader.js:986:32)?[39m

?[90m    at Function.Module._load (internal/modules/cjs/loader.js:879:14)?[39m

?[90m    at Module.require (internal/modules/cjs/loader.js:1026:19)?[39m

?[90m    at require (internal/modules/cjs/helpers.js:72:18)?[39m

    at Object.<anonymous> (C:\nodejs-workspace\nodejs-book\nodebird\app.js:12:20)

?[90m    at Module._compile (internal/modules/cjs/loader.js:1138:30)?[39m

?[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)?[39m

답변 1

답변을 작성해보세요.

0

router.post('/join', async (req, res, next)

입니다.