방법이 함수가 아닙니다 몽구스 방법 문제

Jan 03 2021

ENV :

노드 v12.19.0

몽고 아틀라스 V4.2.11

몽구스 V5.11.8

#############################################

사용자 스키마가 있습니다.

user.js

const mongoose = require('mongoose');
const bcrypt   = require('bcrypt');

const userSchema = new mongoose.Schema({
    email:{
        type: String,
        required: true,
        unique: true, 
    },
    username:{
        type: String,
        required: true,
        unique: true,
    },
    password:{
        type: String,
        required: true
    },
    profileImageUrl:{
        type: String,
    }
});

userSchema.pre('save', async function(next) {
    try{
        if(!this.isModified('password')){
            return next();
        }

        let hashedPassword = await bcrypt.hash(this.password, 10);
        this.password = hashedPassword;
        return next();

    } catch(err) {
        return next(err);
    }
});

userSchema.methods.comparePassword = async function(candidatePassword){
    try{
        return await bcrypt.compare(candidatePassword, this.password);

    } catch(err){
        throw new Error(err.message);
    }
}

userSchema.set('timestamps', true);

module.exports = mongoose.model("User", userSchema);

비밀번호가 수정되지 않았는지 확인한 다음 미리 저장합니다.

그리고 암호를 comparePassword 라는 해시 암호와 비교하는 방법을 추가했습니다.

다른 파일에서 comparePassword 메서드 를 사용하려고 합니다.

Auth.js


const db = require('../models');
const JWT = require("jsonwebtoken");
const CONFIGS = require('../config');

exports.signIn = async function(req, res, next){
    try{

        const user = db.User.findOne({
            email: req.body.email,
        });

        const { id, username, profileImageUrl } = user;
        const isMatch = await user.comparePassword(req.body.password) ; // here is a problem <====

        if(isMatch){
            const token = JWT.sign({
                id,
                username,
                profileImageUrl,
            }, CONFIGS.SECRET_KEY);
            return res.status(200).json({
                id,
                username,
                profileImageUrl,
                token,
            });
        }
        else{
            return next({
                status: 400,
                message: "Invalid email or password",
            });
        }
    }
    catch(err){
        return next(err);
    }
}
   

암호를 미리 정의 된 방법과 비교하려고 할 때 응답에 이것을 반환합니다.

user.comparePassword는 함수가 아닙니다.

다양한 솔루션을 살펴 보았습니다.

일부는 이것이 그들에게 효과가 있다고 말했습니다.

userSchema.method('comparePassword' , async function(candidatePassword, next){
  // the logic
})

하지만 작동하지 않습니다 다른 솔루션을 시도했지만 코드에 무엇이 잘못되었는지 확실하지 않습니다.

업데이트 1 :

정적을 사용해 보았지만 작동하지 않습니다.

userSchema.statics.comparePassword = async function(candidatePassword){
    try{
        return await bcrypt.compare(candidatePassword, this.password);

    } catch(err){
        throw new Error(err.message);
    }
}

답변

AhmedGaafer Jan 04 2021 at 01:12

에서 Auth.js

 const user = db.User.findOne({
            email: req.body.email,
        });

쿼리가 완료 될 때까지 기다려야하기 때문에 이것은 잘못된 것입니다.

이럴거야

 const user = await db.User.findOne({
            email: req.body.email,
        });