Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

api.method is not a function #2

Closed
loetmann opened this issue Sep 7, 2019 · 5 comments
Closed

api.method is not a function #2

loetmann opened this issue Sep 7, 2019 · 5 comments

Comments

@loetmann
Copy link

loetmann commented Sep 7, 2019

Hi,
first of all, I am an absolute beginner to typescript. Allthough excercising a lot of tutorials, this is my first attempt using a foreign module/project.

I stucked, getting:

TypeError: api.method is not a function
    at Object.<anonymous> (/Projekte/typescript/zabbix-client/apiinfo.js:9:19)
    at Module._compile (internal/modules/cjs/loader.js:936:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
    at Module.load (internal/modules/cjs/loader.js:790:32)
    at Function.Module._load (internal/modules/cjs/loader.js:703:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
    at internal/main/run_main_module.js:17:11

no tsc errors, trying to execute via node (node apiinfo.js). Using your example code from the Readme.md

Please could you assist. While an experienced zabbix admin I struggle with typescript, in which I identified a lot of potentials.

Regards
Ralf

@aluisiora
Copy link
Owner

aluisiora commented Sep 7, 2019

Oh, could you give me a snippet of your code?

I'll try to enhance the readme a little bit, but your code should follow a structure like this (JavaScript):

const ZabbixClient = require('zabbix-client').ZabbixClient;

const client = new ZabbixClient('http://zabbix-url/api_jsonrpc.php');

client.login('username', 'password')
.then((api) => {
    
    api.method('host.get')
    .call({})
    .then((allHosts) => {
        console.log(allHosts);
    })
    .catch((e) => {
        console.log(e.message);
    });

})
.catch((e) => {
    console.log(e.message);
});

This example uses Promises, it's not really suited for production but should get you an idea.

If you are not familiarized with javascript yet, it would be good to learn a bit of it before diving into typescript.

@loetmann
Copy link
Author

loetmann commented Sep 8, 2019

Hi Aluisio,
thanks for your support and prompt reply. I am relativly familiar with javascript (in the browser). To be honestly. This is my first attempt writing and executing code for node.js.

Using your snippet ( I am sure its successfully executable) returns

api.method(...).then is not a function

Obviously I am struggling with the project setup and node. So I have to educate myself with more basic exercises concerning this.
Again: Thanks a lot for your support.

Ralf

@loetmann
Copy link
Author

loetmann commented Sep 8, 2019

Hi Aluisio,
what about a complete example as a project in an example folder?
Greetings
Ralf

@aluisiora
Copy link
Owner

I'm terribly sorry, I couldn't test so I wrote the example out of my mind. I forgot to include the call({}) method before then().
The method method() 🤔 returns a ZabbixCommunicator object, which has the call() method that returns a promise.
While method() sets the method from zabbix that you want, call() do the zabbix call with the parameters that you want.

I've updated the example and tested it on my machine.

what about a complete example as a project in an example folder?

Great idea! I'll do it when I got some time.

@loetmann
Copy link
Author

loetmann commented Sep 9, 2019

Hi Aluisio,
this was a big step for ... me. OK, the given Javascript example is a starting for me. I tried hard to get rid of the ts-notation-

In an example, you should also provide how to pass params for the methods.

I'll stay tuned.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants