silas / node-consul

Consul client
https://www.npmjs.com/package/consul
MIT License
559 stars 83 forks source link

Cannot watch services #165

Open magiconair opened 3 days ago

magiconair commented 3 days ago

I am trying to watch service changes like this but get an error. The code works with consul.kv.get as used in the test.

        this.consul = new Consul({ host: host, port: port });
        this.watch = this.consul.watch({
            method: this.consul.catalog.services,
            options: {},
        });
        this.watch.on('change', (data, res) => {
            console.log('watch', 'data', data);
        });
100 |
101 |   /**
102 |    * Lists services in a given DC
103 |    */
104 |   services(...args) {
105 |     return this.service.list(...args);
                      ^
TypeError: undefined is not an object (evaluating 'this.service.list')
      at services (./node_modules/consul/lib/catalog.js:105:17)
      at _run (./node_modules/consul/lib/watch.js:146:8)

Any idea what I'm doing wrong here?

silas commented 3 days ago

Try method: this.consul.catalog.service.list

this.consul.catalog.services is an alias method and doesn't worth with our .call usage:

https://github.com/silas/node-consul/blob/b234a6e79eaa200c08bd265dd3be332e1f744fcf/lib/watch.js#L145-L146