【发布时间】:2020-01-05 20:09:28
【问题描述】:
根据Featherjs Client Authentication docs,我已经在我的 React App 中设置并初始化了模块。然后在我的Login 按钮点击之后,我使用正确的数据格式调用推荐的app.authenticate(data) 方法。这会立即导致客户端包中的feathers.js 文件中的Uncaught TypeError: Cannot read property 'create' of undefined 出现错误。
如果您能指出正确的方向,那将非常有帮助。
对于这个应用程序:
- 我目前正在开发服务器。
- ReactJs 应用位于 localhost:3000
- FeathersJs 应用程序位于 localhost:3030。
- React 应用由 CRA 引导,Feathers 服务器由 CLI 生成。
- 在 React 应用程序中,我使用来自 npm 的
@feathersjs/client包。
我已经尝试通过终端中的curl 请求服务器,并使用正确的凭据进行响应。之后,我通过 React 应用程序发出了 AJAX 请求,这也有效。如果我使用AJAX请求进行认证,我成功获得了用户的token和id。
实际上我可以继续进行下去。但是使用相同的令牌重新验证用户并注销用户时会出现问题。我明白有一些解决方法。但我想使用 FeathersJs 客户端,因为它提供了可以使用的 reAuthenticate 和 logout 方法。
初始化羽毛客户端
import feathers from '@feathersjs/client';
const client = feathers();
client.configure(feathers.authentication({
storage: window.localStorage
}));
export default client;
App.js 中的login 函数调用
login = () => {
const self = this;
client.authenticate({
strategy: 'local',
email: 'hello@robin.com',
password: 'supersecret'
})
.then(data => {
console.log(data);
self.setState({
isAuthenticated: true
});
return data;
})
.catch(err => {
console.log(err);
self.setState({
isAuthenticated: false
});
});
};
调用函数时会抛出以下错误:
在开发控制台中
Uncaught TypeError: Cannot read property 'create' of undefined
at AuthenticationClient.authenticate (feathers.js:2838)
at Object.App.login (App.js:50)
at onClick (Login.js:8)
在运行ReactJs应用的浏览器中,显示如下错误:
TypeError: Cannot read property 'create' of undefined
AuthenticationClient.authenticate
node_modules/@feathersjs/client/dist/feathers.js:2838
> 2838 | var promise = this.service.create(authentication).then(function (authResult) {
我做错了什么,如何使用 FeathersJs 客户端?
【问题讨论】:
标签: reactjs feathersjs feathers-authentication