Feathersjs API ES6类挂钩和休息未定义

时间:2016-12-19 16:07:41

标签: javascript node.js ecmascript-6 feathersjs

我正在使用ES6类设置一个feat-client,我想在我的React-Redux-Webpack应用程序的操作中导入它。

我使用了很棒的Feathers脚手架设置了一个REST api。

不幸的是,我在浏览器中收到错误,导致我的一天毁了。我做错了什么?

  

未捕获的TypeError:_client2.default.hooks不是函数

任何人都可以帮助我吗?为什么hooks(以及rest)在这里未定义?软件包似乎安装得正确......

以下是一个好主意吗?

// src/middleware/api.js

import hooks from 'feathers-hooks'
import feathers from 'feathers/client'
import rest from 'feathers-rest/client'

class API {
  constructor() {
    const connection = process.env.FEATHERS_API_URL
    this.app = feathers()
      .configure(feathers.hooks())
      .configure(rest(connection).fetch(fetch))
      .configure(feathers.authentication({
        type: 'local',
        storage: window.localStorage,
      }))
  }
}

是不是有些软件包不兼容?

"feathers": "^2.0.3",
    "feathers-authentication": "^0.7.12",
    "feathers-client": "^1.8.0",
    "feathers-configuration": "^0.3.3",
    "feathers-errors": "^2.5.0",
    "feathers-hooks": "^1.7.1",
    "feathers-rest": "^1.5.2",
    "feathers-sequelize": "^1.4.0"

我想知道的另一件事是,我们是否始终需要为rest函数提供路径?它可以默认使用配置文件中使用的路径吗?在同一个项目中同时将客户端和服务器端代码放在一条路上感觉有点奇怪......

1 个答案:

答案 0 :(得分:1)

feathers/clientfeathers-client包之间存在差异。 feathers-client是一个捆绑包,包含一组Feathers标准模块(如hooks,auth,rest,socketio),可以直接在浏览器中使用(参见the documentation here)。

看起来您正在尝试使用模块加载器which is documented here,而不是使用预绑定的包(其中所有内容都在feathers.名称空间中,如feathers.hooks,{{1您只能导入所需的模块并进行配置:

feathers.authentication
如果

// src/middleware/api.js import hooks from 'feathers-hooks' import feathers from 'feathers/client' import rest from 'feathers-rest/client' import authentication from 'feathers-authentication/client' class API { constructor() { const connection = process.env.FEATHERS_API_URL this.app = feathers() .configure(hooks()) .configure(rest(connection).fetch(fetch)) .configure(authentication({ type: 'local', storage: window.localStorage, })) } } 在同一个域上运行,则不需要基本网址。默认情况下它是一个空字符串。