1

I'm trying to extends the KUZZLE JavaScript SDK in order to call some controllers on kuzzle servers, implemented via plugins. I'm following that guide: add controller

Here is my controller which extends from the 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
 }
}
}

And here is where I specify the controller in order to use it further in the App, on the creation of the singleton.

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;

Somehow I'm getting the following error:

Controllers must inherit from the base controller

Is there something wrong with the imports ?

Aschen
  • 1,691
  • 11
  • 15
Hubert Solecki
  • 2,611
  • 5
  • 31
  • 63

1 Answers1

1

I've found out the solution to that issue. Firstly, I was not on the right version of the kuzzle SDK released recently (6.1.1) and secondly the controller class must be exported as default:

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
    }
 }
 }

And then the UserController needs to be importer that way:

import UserController from './UserController.js'

Then, as specified in the documentation, we need just inject the kuzzle object into the controller that way:

kuzzle.useController(UserController, 'user');
Hubert Solecki
  • 2,611
  • 5
  • 31
  • 63