gpt4 book ai didi

javascript - kuzzle/react native - 无法从 BaseController 继承类

转载 作者:行者123 更新时间:2023-11-30 19:34:39 25 4
gpt4 key购买 nike

我正在尝试扩展 KUZZLE JavaScript SDK,以便调用 kuzzle 服务器上的一些 Controller ,这些 Controller 是通过插件实现的。我正在遵循该指南:add controller

这是我的 Controller ,它从 BaseController 扩展而来:

const { BaseController } = require('kuzzle-sdk');

export class UserController extends BaseController {
constructor (kuzzle) {
super(kuzzle, 'plugins-user/userController');
}

/**
* Method to call the action "CreateAccount" on the UserController
* @param {*} user
*/
async createAccount(user) {
const apiRequest = {
action: 'new',
body: {
user
}
};

try {
const response = await this.query(apiRequest);
return response.result.user;
}
catch (error) {
//Manage errors
}
}
}

这里是我指定 Controller 的地方,以便在创建单例时在应用程序中进一步使用它。

const {UserController} = require('./UserController');
const { Kuzzle, WebSocket } = require('kuzzle-sdk');

class KuzzleService {

static instance = null;
static async createInstance() {
var object = new KuzzleService();
object.kuzzle = new Kuzzle(
new WebSocket('localhost'),{defaultIndex: 'index'}
);
object.kuzzle.useController(UserController, 'user');

await object.kuzzle.connect();
const credentials = { username: 'admin', password: 'pass' };
const jwt = await object.kuzzle.auth.login('local', credentials);
return object;
}

static async getInstance () {
if (!KuzzleService.instance) {
KuzzleService.instance = await KuzzleService.createInstance();
}
return KuzzleService.instance;
}

}
export default KuzzleService;

不知何故我收到以下错误:

Controller 必须继承自基础 Controller

进口有问题吗?

最佳答案

我已经找到了解决该问题的方法。首先,我使用的不是最近发布的 kuzzle SDK 的正确版本 (6.1.1),其次, Controller 类必须导出为默认:

const { BaseController } = require('kuzzle-sdk');

export default class UserController extends BaseController {
constructor (kuzzle) {
super(kuzzle, 'plugins-user/userController');
}

/**
* Method to call the action "CreateAccount" on the UserController
* @param {*} user
*/
async createAccount(user) {
const apiRequest = {
action: 'new',
body: {
user
}
};

try {
const response = await this.query(apiRequest);
return response.result.user;
}
catch (error) {
//Manage errors
}
}
}

然后 UserController 需要以这种方式导入:

import UserController from './UserController.js'

然后,按照文档中的说明,我们只需将 kuzzle 对象以这种方式注入(inject)到 Controller 中:

kuzzle.useController(UserController, 'user');

关于javascript - kuzzle/react native - 无法从 BaseController 继承类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56068746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com