Javascript 无法设置未定义的属性statusCode
我正在尝试为我的应用程序创建后端,实际上我正在为用户编写路由登录,但在使用Postman测试路由时遇到错误,此错误为: 未处理的PromisejectionWarning:TypeError:无法设置属性 未定义的“statusCode” 排队 this.response.statusCode=401 这是我的代码:Javascript 无法设置未定义的属性statusCode,javascript,node.js,mongodb,api,express,Javascript,Node.js,Mongodb,Api,Express,我正在尝试为我的应用程序创建后端,实际上我正在为用户编写路由登录,但在使用Postman测试路由时遇到错误,此错误为: 未处理的PromisejectionWarning:TypeError:无法设置属性 未定义的“statusCode” 排队 this.response.statusCode=401 这是我的代码: import Rider from '../../models/riders' import jwt from 'jsonwebtoken' export default cla
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此响应的第一个声明中有一个输入错误
this.reponse => this.response
此响应似乎未定义。你的路线在哪里?应该有对(req,res)
的引用,谢谢,那只是。。。很抱歉。@Pixelyoda,如果答案是肯定的,你可以接受我的答案。谢谢,但是现在很奇怪,CheckIdentificationant中的findOne方法为rider返回了一个空对象,但是我已经在本地环境中运行了我的数据库,架构rider和数据都存在。。。