• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    미해결

unresolved methods

20.08.21 21:58 작성 조회수 343

0

안녕하세요

user.js에서

userSchema.pre

userScema.methods

var token = jwt.sign(user._id.toHexString(), secretToken) 에서

pre, methods, toHexString() 세 부분에 밑줄이 뜨며 unresolved methods or functions 등 unresolved 라는 에러가 뜹니다. 작동에는 오류가 없지만 왜 뜨는건지 궁금해서 질문드립니다.

웹스톰 사용하고 있고, 

위의 사진과 같습니다.

감사합니다!!

//user.js

const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const saltRounds = 10;
const jwt = require('jsonwebtoken');

const userSchema = new mongoose.Schema({
    name: {
        type: String,
        maxlength: 50
    },
    email: {
        type: String,
        trim: true,     //공백을 제거
        unique: 1 //이메일은 하나. 유니크 했으면 좋겠음.
    },
    password: {
        type: String,
        minlength:5
    },
    lastname: {
        type: String,
        maxlength: 50
    },
    role: {
        type: Number,
        default: 0
    },
    image: String,      //오브젝트를 사용하지 않고 이렇게 하나로 줘도 된다.
    token: {
        type: String    //유효성 검사를 위해
    },
    tokenExp: {
        type: Number        //유효기간.
    }
})

userSchema.pre('save', function(next){
    //비밀번호를 암호화 시킨다
    //index.js 의 save를 하기 전에, function을 하면서 암호화 시킨다.
    //이때 salt를 이용해서 비밀번호를 암호화한다. 그 전에 salt를 먼저 생성해야함.
    //saltround는 salt가 몇글자인지임
    //genSalt: salt를 만든다.

    var user = this;        //this는 이때 위의 user 객체를 가리킨다.

    if(user.isModified('password')){
        bcrypt.genSalt(saltRounds, function(err, salt){
            if (err) return next(err);
            bcrypt.hash(user.password, salt, function (err, hash){
                //user.password : this, 즉 객체의 password. 암호화 되기 전의 패스워드
                //hash : 암호화된 비밀번호
                //store hash in your password DB
                if (err) return next(err)
                user.password = hash
            })
        })
    } else{
        next()
    }


    next()

})

userSchema.methods.comparePassword = function(plainPassword, cb) {
    //plainpassword와 암호화된 비밀번호가 같은지 확인하기 -> plainpassword를 암호화 해서 암호화한 password와 같은지 확인하기.
    bcrypt.compare(plainPassword, this.password, function(err, isMatch){
        if(err) return cb(err),
             cb(null, isMatch)
    })
}


userSchema.methods.generateToken = function(cb){

    var user = this;

    //jsonwebtoken을 이용해서 token을 생성하기
    var token = jwt.sign(user._id.toHexString(), 'secretToken')
    user.token = token
    user.save(function(err, user){
        if(err) return cb(err)
        cb(null, user)
    })
}

const User = mongoose.model('User', userSchema)     //스키마를 모델로 감싸준다

module.exports = { User }     //다른 파일에서도 쓸 수 있도록 export

답변 1

답변을 작성해보세요.

0

안녕하세요 ~!    웹스톰도 인터페이스가 깔끔한것 같네요 ~ !  그런데 제가 웹스톰을 써본적이 없어서 ㅠㅠ  
우선 구글링해보니 

https://stackoverflow.com/questions/20136714/how-can-i-fix-webstorm-warning-unresolved-function-or-method-for-require-fi

이런식에 글이 있더라구요 ~!    들어가서 따라해보시면 될 것 같습니다 ~ !