简体   繁体   中英

"TypeError: Cannot read property 'status' of undefined" node.js

im trying to make a function that return a token using nodejs express.js sequelize and i got this error "TypeError: Cannot read property 'status' of undefined"

exports.login = (data)=>{
   return user.findOne({
        where : {userName: data.userName}
    }).then(userFound => {
        if (userFound){

            bcrypt.compare(data.password,userFound.password,(errBcrypt,resBcrypt)=>{
                if(resBcrypt) {
                    return {
                        status : 201,
                        token : jwt.sign({id: user.id},secretCode,{expiresIn:'1h'})
                    };
                }else {
                    return {
                        status : 403,
                        errBcrypt : 'password invalid!'
                    };
                }
            })

        }else{
            return {
                status : 403,
                error : 'user not exist!'
            };
        }

    })
    .catch((err) =>{
        return {
            status : 403,
            error : err
        };
    });

}

is that a controller function or where is it used exactly also try maybe returning a promise

    exports.login = (data)=>{
   return new Promise((resolve,reject)=>{
   user.findOne({
        where : {userName: data.userName}
    }).then(userFound => {
        if (userFound){

            bcrypt.compare(data.password,userFound.password,(errBcrypt,resBcrypt)=>{
                if(resBcrypt) {
                    return resolve({
                        status : 201,
                        token : jwt.sign({id: user.id},secretCode,{expiresIn:'1h'})
                    })
                }else {
                     return resolve({
                        status : 403,
                        errBcrypt : 'password invalid!'
                    })
                }
            })

        }else{
             return resolve({
                status : 403,
                error : 'user not exist!'
            })
        }

    })
    .catch((err) =>{
         return reject({
            status : 403,
            error : err
        })
    });


   }) 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM