• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    미해결

"TypeError: Cannot read property 'findOne' of undefined"가 발생합니다.

19.04.26 11:04 작성 조회수 809

0

signup은 정상적으로 동작하지만 login을 진행할 때 "TypeError: Cannot read property 'findOne' of undefined"가 발생합니다.

 

디버깅을 해보니까

 

passport.authenticate('local', (authError, user, info) => {

console.log(user)
})

안에서 user가 undefined입니다.

 

결국 passport의 localStrategy에서 문제가 발생하는 것 같습니다.

 

또 localStrategy에서

{ usernameField: 'email', passwordField: 'password' }

를 지우면 findOne 에러는 발생하지 않습니다. 하지만 이 경우에는 passport.authenticate 안에서 user를 찍어보면 false가 발생합니다.

 

디비는 몽고디비를 사용중입니다.

답변 7

·

답변을 작성해보세요.

1

구조분해 할당을 할 이유가 없습니다. 객체나 배열만 구조분해 할당하는 것입니다.

1

몽고디비라서 구조상

const User = require('../model/user') 해야할 것 같내요.

0

자린이님의 프로필

자린이

질문자

2019.05.01

아! 해결이 됐습니다. 감사합니다. 몽고디비라서 비구조화 할당에 문제가 생기는 이유를 알 수 있을까요?

0

자린이님의 프로필

자린이

질문자

2019.04.30

const LocalStrategy = require('passport-local').Strategy;
const { User } = require('../model/user');

module.exports = (passport) => {
  passport.use(
    new LocalStrategy(
      {
        usernameField: 'email',
        passwordField: 'password'
      },
      async (email, password, done) => {
        // done(에러, 성공, 실패)
        try {
          console.log('어디가 에러나는거야');
          const exUser = await User.findOne({ email: email });
          if (exUser) {
            // 비밀번호 검사
            const result = await bcrypt.compare(password, exUser.password);
            if (result) {
              done(null, exUser);
            } else {
              done(null, false, { message: '비밀번호가 일치하지 않습니다.' });
            }
          } else {
            done(null, false, { message: '가입되지 않은 회원입니다.' });
          }
        } catch (err) {
          console.error(err);
          done(err);
        }
      }
    )
  );
};

localstrategy.js 코드입니다.

네, authenticate 안에서는 findOne을 해주지 않았습니다.

0

localStrategy 코드를 올려주세요.

authenticate 안에서 user.findOne을 하신건 아니죠?

0

자린이님의 프로필

자린이

질문자

2019.04.28

DB는 몽고디비 사용했습니다 ㅠ_ㅠ..

0

아건 시퀄라이즈 버전 문제입니다. 5 버전을 설치하신 것 같은데 4버전을 설치하세요.

npm i sequelize@4