Feathers 客户端(@feathers/客户端)无法在 ReactJs 应用程序中验证用户名/密码



根据Featherjs客户端身份验证文档,我已经在我的React App中设置并初始化了模块。之后,单击Login按钮后,我使用正确的数据格式调用推荐的app.authenticate(data)方法。这会导致客户端包中的feathers.js文件中立即Uncaught TypeError: Cannot read property 'create' of undefined错误。

如果您能指出我正确的方向,那将非常有帮助。

对于此应用:

  1. 我目前正在开发服务器上工作。
  2. ReactJs 应用程序在本地主机上:3000
  3. 本地主机:3030 上的 FeathersJs 应用程序。
  4. React 应用程序使用 CRA 引导,Feathers 服务器由 CLI 生成。
  5. 在 React 应用程序中,我使用的是 npm 的@feathersjs/client包。

我已经尝试通过终端中的curl请求服务器,并使用正确的凭据进行响应。之后,我通过 React 应用程序发出了 AJAX 请求,这也有效。如果我使用 AJAX 请求进行身份验证,则成功获取用户的tokenid

实际上,我可以继续前进。但是,使用相同的令牌重新验证用户并注销用户时会出现此问题。我确实知道有解决方法。但我想使用 FeathersJs 客户端,因为它提供了现成的reAuthenticatelogout方法。

初始化羽毛客户端

import feathers from '@feathersjs/client';
const client = feathers();
client.configure(feathers.authentication({
storage: window.localStorage
}));
export default client;

应用程序中login函数.js称为

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客户端?

你忘了初始化 REST 或 Socket.io 客户端连接,如 React Native API 所示。它应该是

import io from 'socket.io-client';
import feathers from '@feathersjs/feathers';
import socketio from '@feathersjs/socketio-client';
const socket = io('http://api.my-feathers-server.com', {
transports: ['websocket'],
forceNew: true
});
const client = feathers();
client.configure(socketio(socket));
client.configure(feathers.authentication({
storage: window.localStorage
}));
export default client;

相关内容

最新更新