1

I'm using this library in a TypeScript project. And this is how my class looks like:

import OnvifManager from 'onvif-nvt'
Class OnvifApi {
// device: any = undefined
device = {} as OnvifDevice
constructor (...params) {
// definition
}
connect (): Promise<any> {
  OnvifManager.connect(...params).then((response: OnvifDevice) => {
    this.device = response
    resolve(response)
  }
}
coreService(): Promise<Type[]> {
  this.device.add('core')
}
}

And this this interface I created for the response from onvif-nvt

interface OnvifDevice {
  address: string
  // type
  // type
  // type
  add(name: string): void
}

This class is always giving an error in coreService saying that

this.device.add is not a function.

EDIT: This is the return of the connect method, which returns the whole Camera object.

Things are working when device is defined to any.

How can I do this interface mapping from JavaScript to TypeScript?

Response from connect

10
  • Are you sure the connect is giving you an object with an add function? That runtime error would seem to indicate otherwise. Have you debugged to check what the response object is? Commented Aug 30, 2021 at 18:43
  • Have you also checked to make sure coreService is not called before connect resolves? Commented Aug 30, 2021 at 18:46
  • Please don't use images to show textual information. Use text to show textual information. Commented Aug 30, 2021 at 18:51
  • I added the response object in question. And yes, coreService is called after connect resolves. Everything works when device is of type any. Commented Aug 30, 2021 at 18:52
  • 2
    Shouldn't it be device: OnvifDevice = {} instead of device = OnvifDevice = {}? Or what are you trying to do here? And why is Class uppercase? Commented Aug 30, 2021 at 18:53

1 Answer 1

2

The reason is, in the interface, OnvifDevice, add is a mandatory parameter, but the device object is initialised with an empty object.

The dynamic mapping at this.device = response is not happening. Also please check if the response has add method or not while you debug.

These are the two possible symptoms I see.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.