Js2Py icon indicating copy to clipboard operation
Js2Py copied to clipboard

gun.js node module import -> TypeError: 'undefined' is not a function

Open bitnom opened this issue 5 years ago • 0 comments
trafficstars

I setup a project here: https://github.com/TensorTom/gun-native#attempt-2---js2py-some-success

I would like to be able to use the js library gun as gun.put({'foo': 'bar'}) and then gun.get('foo') and have it actually work. Here's a simple example of how to use gun: https://repl.it/talk/share/gun-example/50583

const Gun = require('gun')

const gun = Gun([
  'https://mvp-gun.herokuapp.com/gun',
  'https://e2eec.herokuapp.com/gun'
])

gun.get('baz').put({foo: 'bar'})

gun.get('baz').once(console.log)

When I load it with Js2Py, it seems to be semi-working but I can't seem to put and get from the gun db/graph. With the following code:

import js2py
Gun = js2py.require('gun')
import json


def handle_get(*args):
    print(args)


def main():
    # Grab reference to Gun
    #Gun = js2py.eval_js('Gun')
    # Init Gun & connect to our superPeer
    gun = Gun('http://localhost:8089/gun')
    # Get a reference to a node to work with
    baz = gun.get('baz')
    # Hopefully put some data to the graph
    baz.put(json.dumps({ 'foo': 'bar' }))
    # With luck we will get it back
    gun.get('baz').once(handle_get)


if __name__ == "__main__":
    main()

I get:

'Hello wonderful person! :) Thanks for using GUN, please ask for help on http://chat.gun.eco if anything takes you longer than 5min to figure out!'
'Warning: No localStorage exists to persist data to!'
'Native Object.prototype polluted, reverting'
'Data saved to the root level of the graph must be a node (an object), not a'
Traceback (most recent call last):
  File "gun.py", line 24, in <module>
    main()
  File "gun.py", line 20, in main
    gun.get('baz').once(handle_get)
  File "/home/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/js2py/base.py", line 1203, in __call__
    return to_python(parent._obj.callprop(meth, *args))
  File "/home/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/js2py/base.py", line 996, in callprop
    return cand.call(self, args)
  File "/home/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/js2py/base.py", line 1464, in call
    return Js(self.code(*args))
  File "<string>", line 14050, in PyJs_anonymous_1181_
  File "/home/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/js2py/base.py", line 996, in callprop
    return cand.call(self, args)
  File "/home/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/js2py/base.py", line 1464, in call
    return Js(self.code(*args))
  File "<string>", line 13527, in PyJs_anonymous_1160_
  File "/home/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/js2py/base.py", line 996, in callprop
    return cand.call(self, args)
  File "/home/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/js2py/base.py", line 1464, in call
    return Js(self.code(*args))
  File "<string>", line 11608, in PyJs_onto_1082_
  File "/home/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/js2py/base.py", line 996, in callprop
    return cand.call(self, args)
  File "/home/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/js2py/base.py", line 1464, in call
    return Js(self.code(*args))
  File "<string>", line 13093, in PyJsHoisted_output_
  File "/home/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/js2py/base.py", line 996, in callprop
    return cand.call(self, args)
  File "/home/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/js2py/base.py", line 1464, in call
    return Js(self.code(*args))
  File "<string>", line 12282, in PyJs_ask_1118_
  File "/home/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/js2py/base.py", line 949, in __call__
    return self.call(self.GlobalObject, args)
  File "/home/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/js2py/base.py", line 1464, in call
    return Js(self.code(*args))
  File "<string>", line 10951, in PyJs_anonymous_1044_
  File "/home/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/js2py/base.py", line 947, in __call__
    raise MakeError('TypeError',
js2py.internals.simplex.JsException: TypeError: 'undefined' is not a function

bitnom avatar Aug 21 '20 23:08 bitnom