TypeError:无法读取属性' then'使用JWT代币和刷新令牌时的不确定
抱歉,我是后端(Nodejs)的新手,我正在尝试构建一个JWT令牌和刷新令牌,但我偶然发现了一个错误,当我尝试运行失眠症的终点时,我无法解决。
typeError:无法读取属性'然后'''''''''
这是该错误来自我的app.js文件中的代码段
app.post('/users/login', (req, res) => {
let email = req.body.email;
let password = req.body.password;
User.findByCredentials(email, password)
.then((user) => {
return user.createSession()
.then((refreshToken) => {
return user.generateAccessAuthToken()
.then((accessToken) => {
return { accessToken, refreshToken };
});
})
.then((authTokens) => {
res
.header('x-refresh-token', authTokens.refreshToken)
.header('x-access-token', authTokens.accessToken)
.send(user);
})
}).catch((e) => {
res.status(400).send(e);
});
});
,这是我user.model.js文件中方法“ FindbyCredentials”的代码,
UserSchema.statics.findByCredentials = function(email, password) {
let User = this;
User.findOne({ email }).then((user) => {
if(!user) return Promise.reject();
return new Promise((resolve, reject) => {
bcrypt.compare(password, user.password, (err, res) => {
if (res) resolve(user);
else {
reject();
}
})
})
});
}
请帮助我
解决错误:我只需要返回FindbyCredentials方法的值即可解决问题
Sorry I'm new to backend (nodejs) and I'm trying to build a JWT token and refresh token but I stumbled across an error that I cannot fix when I try to run an endpoint in insomnia.
TypeError: Cannot read property 'then' of undefined
This is the code snippet from the area the error came from in my app.js file
app.post('/users/login', (req, res) => {
let email = req.body.email;
let password = req.body.password;
User.findByCredentials(email, password)
.then((user) => {
return user.createSession()
.then((refreshToken) => {
return user.generateAccessAuthToken()
.then((accessToken) => {
return { accessToken, refreshToken };
});
})
.then((authTokens) => {
res
.header('x-refresh-token', authTokens.refreshToken)
.header('x-access-token', authTokens.accessToken)
.send(user);
})
}).catch((e) => {
res.status(400).send(e);
});
});
And this is the code for the method "findByCredentials" in my user.model.js file
UserSchema.statics.findByCredentials = function(email, password) {
let User = this;
User.findOne({ email }).then((user) => {
if(!user) return Promise.reject();
return new Promise((resolve, reject) => {
bcrypt.compare(password, user.password, (err, res) => {
if (res) resolve(user);
else {
reject();
}
})
})
});
}
Please help me
Error solved: I just needed to return the value of the findByCredentials method to solve the problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于没有发布错误,因此很难查明错误行&原因。
但是我建议使用.catch块以及每个链的末端,然后获得错误的范围/错误
建议:
您正在通过使用嵌套。然后链条什么是什么回叫地狱。
尝试使用Async/等待语法以简化的方式实现相同的功能 - ref 。
问候,
穆罕默德
Since the error isn't posted, it's hard to pinpoint the error line & cause.
But I would suggest using .catch blocks along with the end of every .then chain and get the spot/line of error
Recommendation:
You are creating a callback hell by using nested .then chains what is call back hell.
Try using async/await syntax to achieve the same functionality in a simplified way - Ref.
Regards,
Muhamed
好像您没有发送任何代码段,这很清楚。但是,在浏览了您的代码后,似乎在解决“用户”后,您正在调用两种方法创建和generateaccessauthtoken。而且,由于您也没有发送这些方法的任何代码段,所以我假设您正在从其中一个功能中出现错误。
和一个建议:
练习最佳错误处理模式。它将为您的旅程提供帮助。
https://wwwww.youtube.com/watch?v=cftftueq-10
和异步JS:
https://www.youtube.com/watch?v=porjizfvm7s
As if you didn't send any code snippet, it is quite clear. But after going through your code it seems after resolving the "user" you are calling two methods createSession and generateAccessAuthToken. And as you also didn't send any code snippet of these methods I'm assuming you are having error from one of the functions.
And a piece of advice:
Practice best error handling patterns. It will help you in your journey.
https://www.youtube.com/watch?v=cFTFtuEQ-10
And async js:
https://www.youtube.com/watch?v=PoRJizFvM7s