eclipse-thingweb / node-wot

A fast and extensible framework to connect any device with your browser and backend applications
https://thingweb.io
Other
166 stars 80 forks source link

TypeError: thing.addAction is not a function #174

Closed jenlincode closed 4 years ago

jenlincode commented 4 years ago

When I run:

node packages/cli/dist/cli.js examples/scripts/example-event.js

I get the error:

node packages/cli/dist/cli.js examples/scripts/example-event.js

WoTImpl producing new ExposedThing 'EventSource'
Servient generating ID for 'EventSource': 'urn:uuid:4af9ba69-db0d-48f5-bb71-481a432830d5'
Servient reset ID 'urn:uuid:4af9ba69-db0d-48f5-bb71-481a432830d5' with 'EventSource'
Script error:  TypeError: thing.addAction is not a function
    at /mnt/c/Users/jade/Documents/GovTech/thingweb.node-wot/examples/scripts/example-event.js:22:9
    at Script.runInContext (vm.js:131:20)
    at DefaultServient.Servient.runPrivilegedScript (/mnt/c/Users/jade/Documents/GovTech/thingweb.node-wot/packages/core/dist/servient.js:153:20)
    at /mnt/c/Users/jade/Documents/GovTech/thingweb.node-wot/packages/cli/dist/cli.js:85:38
    at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:63:3)

There's likely something obvious that I'm forgetting, as this is one of the default example files that's included with node-wot. Just curious what I may be missing.

jenlincode commented 4 years ago

I get the same type of error when I try to call:

thing.setPropertyReadHandler() on the client side. Let me know if there's a dependency I may be missing?

jenlincode commented 4 years ago

Also when I try to run the client script:

node packages/cli/dist/cli.js --clientonly examples/scripts/example-event-client.js

WoT-Servient using defaults as 'wot-servient.conf.json' does not exist
DefaultServient configured with
{
  servient: { clientOnly: true, scriptAction: false },
  http: { port: 8080, selfSigned: false },
  coap: { port: 5683 }
}
DefaultServient started
WoTImpl producing new ExposedThing 'servient'
Servient generating ID for 'servient': 'urn:uuid:8996f0b5-dff7-457e-981c-7aa523fff101'
Servient reset ID 'urn:uuid:8996f0b5-dff7-457e-981c-7aa523fff101' with 'servient'
ExposedThing 'servient' setting action Handler for 'log'
ExposedThing 'servient' setting action Handler for 'shutdown'
ExposedThing 'servient' setting action Handler for 'runScript'
ExposedThing 'servient' setting read handler for 'things'
ExposedThing 'servient' exposing all Interactions and TD
Servient has no servers to expose Things
serializeTD() produced

{"title":"servient","description":"node-wot CLI Servient","properties":{"things":{"type":"object","description":"Get things","observable":false,"readOnly":true,"writeOnly":false}},"actions":{"log":{"description":"Enable logging","input":{"type":"string"},"output":{"type":"string"},"idempotent":false,"safe":false},"shutdown":{"description":"Stop servient","output":{"type":"string"},"idempotent":false,"safe":false},"runScript":{"description":"Run script","input":{"type":"string"},"output":{"type":"string"},"idempotent":false,"safe":false}},"@context":"https://www.w3.org/2019/wot/td/v1","@type":"Thing","security":["nosec_sc"],"id":"urn:uuid:8996f0b5-dff7-457e-981c-7aa523fff101","securityDefinitions":{"nosec_sc":{"scheme":"nosec"}}}

WoT-Servient loading 1 command line script
WoT-Servient reading script examples/scripts/example-event-client.js
WoT-Servient running script '/********************************************************************************'... (35 lines)
Servient caught error in privileged '/mnt/c/Users/jade/Documents/GovTech/thingweb.node-wot/examples/scripts/example-event-client.js' and halted with /mnt/c/Users/jade/Documents/GovTech/thingweb.node-wot/examples/scripts/example-event-client.js:16
WoT.fetch("http://localhost:8080/EventSource").then( async (td) => {
    ^

TypeError: WoT.fetch is not a function
    at /mnt/c/Users/jade/Documents/GovTech/thingweb.node-wot/examples/scripts/example-event-client.js:16:5
    at Script.runInContext (vm.js:131:20)
    at DefaultServient.Servient.runPrivilegedScript (/mnt/c/Users/jade/Documents/GovTech/thingweb.node-wot/packages/core/dist/servient.js:153:20)
    at /mnt/c/Users/jade/Documents/GovTech/thingweb.node-wot/packages/cli/dist/cli.js:85:38
    at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:63:3)
egekorkan commented 4 years ago

There are two subproblems here:

jenlincode commented 4 years ago

You would need to use WoTHelpers instead of WoT, remove anything like addAction, addProperty etc.

So for example-event.js, addAction should be replaced with setActionHandler()? Just curious, but can the action be dynamically defined in example-event.js without the action being set up in "actions" part?

And I presume for example-event-client.js, replacing WoT with WoTHelpers would make it work?

Thanks for explaining some methods should only be set up on the server side -- I feel like some of that needs to almost be highlighted in another color in the documentation, since in wall-of-text documentation, I admittedly tend to skip over fine details. 😅

egekorkan commented 4 years ago

And I presume for example-event-client.js, replacing WoT with WoTHelpers would make it work?

Yes :)

So for example-event.js, addAction should be replaced with setActionHandler()? Just curious, but can the action be dynamically defined in example-event.js without the action being set up in "actions" part?

You don't need to replace it with something, you can simply remove it. Before, we were adding each interaction with a method but now you just put a TD in the produce( ) method and it adds all the interactions you have put in the TD.

jenlincode commented 4 years ago

Thanks for all the support, Ege! Closing this issue now.

danielpeintner commented 4 years ago

FYI: we are working on a PR to update all examples.