我正在尝试扩展KUZZLE JavaScript SDK,以便调用通过插件实现的Kuzzle服务器上的某些控制器。
我正在遵循该指南:add 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
}
}
}
在这里,我指定了控制器,以便在创建单例时在App中进一步使用它。
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;
不知何故,我收到以下错误:
控制器必须从基本控制器继承
进口有问题吗?
最佳答案
我已经找到解决该问题的方法。首先,我使用的不是最新发布的kuzzle SDK的正确版本(6.1.1),其次,控制器类必须导出为默认值:
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对象以这种方式注入到控制器中:
kuzzle.useController(UserController, 'user');