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



im试图使用nodejs-express.jssequelize创建一个返回令牌的函数我犯了这个错误"TypeError:无法读取未定义"的属性"status">

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
        };
    });
}

控制器函数或它的确切使用位置也可以尝试返回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
        })
    });

   }) 
}

相关内容

最新更新