0

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

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

对于这个应用程序:

  1. 我目前正在开发服务器。
  2. ReactJs 应用程序位于 localhost:3000
  3. FeathersJs 应用程序位于 localhost:3030。
  4. React 应用程序使用 CRA 引导,Feathers 服务器由 CLI 生成。
  5. 在 React 应用程序中,我使用@feathersjs/client来自 npm 的包。

我已经尝试通过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;

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

4

1 回答 1

1

您忘记初始化RESTSocket.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;
于 2019-09-03T19:56:44.890 回答