我正在尝试为我的应用程序创建一个后端,实际上我正在为用户编写路由登录,但是当我用Postman测试路由时遇到错误,该错误是:
UnhandledPromiseRejectionWarning:TypeError:无法设置属性 未定义的“ statusCode”
在线
this.response.statusCode = 401
这是我的代码:
import Rider from '../../models/riders'
import jwt from 'jsonwebtoken'
export default class LoginService {
/**
* @param {String} email
* @param {String} password
* */
constructor(email, password) {
this.email = email
this.password = password
this.reponse = {
statusCode: 200,
responseData: {},
}
}
loginRider() {
return new Promise((resolve) => {
this.checkIdentifiant()
.then((r) => {
// identifiant correct, l'user peut se connecter
console.log(r)
console.log("apres then apres checkidentifiant")
this.response.responseData = {
token: jwt.sign(r.toJSON(), process.env.TOKEN_PASSWORD),
}
resolve(this.response)
})
.catch((e) => {
console.log("dans le catch login rider")
console.log(e)
// erreur lors de l'authentification / error code unauthorized 401
this.response.statusCode = 401
this.response.responseData = {
error: 'unauthorized',
error_description: 'Identifiants incorrect',
}
// On log l'erreur dans la console pour l'admin
resolve(this.response)
})
})
}
/**
* Vérifie le mot de passe et l'email de l'user qui tente de se connecter
*
* @returns {boolean}
*/
checkIdentifiant() {
// on cherche un livreur ayant le même mail que celui fourni dans la BDD
return new Promise((resolve, reject) => {
Rider.findOne({ email: this.email, password: this.password }, (err, rider) => {
if (err || !rider) {
console.log("identifiant invalide dans le checkidentifiant")
reject()
}
// si le rider est trouvé dans la BDD
if (rider) {
console.log("identifiant correct, dans le check identifiant")
// pas sécurisé, à améliorer
resolve(rider)
}
})
})
}
}
我放置的console.log向我展示了我在LoginRider的拦截中运行。
我使用nodeJS和expressJS编写后端,而我的数据库使用MongoDB。 Postman用于测试API
答案 0 :(得分:0)
this.response的第一个声明中有错字
space