Search code examples
node.jsdartdart2js

Using dart2js output with Cloud Code by Parse.com


First, I transpose the javascript example into a dart one.

JS

Parse.Cloud.define('hello', function(request, response) {
  response.success('Hello world');
});

DART

import 'dart:js' show allowInterop;
import 'package:js/js.dart' show JS;

@JS('Parse.Cloud.define')
external void define(String name, Function func);

void main() {
  define('hello', allowInterop((req, res) {
    res.success('Yeah!');
  }));
}

Then I compile it using dart2js (with csp or not).

Finally I run parse deploy and I get

ReferenceError: self is not defined
    at <error: TypeError: Object #<error> has no method '__lookupGetter__'>
    at main.js:2539:9
    at init.currentScript (main.js:2519:7)
    at main.js:2534:5
    at main.js:2542:3

and now, here I am...

How I could get it work on parse.com which is a nodejs env I presume.


Solution

  • self is effectively not defined in the environement provided by parse.com, so I defined self such as var self = this; in the dart2js output.

    I get a new error, about success$1 is not defined. Well, that's true, my code is still incomplet...

    Dart code should like this:

    import 'dart:js' show allowInterop;
    import 'package:js/js.dart' show JS, anonymous;
    
    @JS('Parse.Cloud.define')
    external void define(String name, Function func);
    
    @JS()
    @anonymous
    class HttpResponse {
      external void success(String msg);
      external void error(String msg);
    }
    
    void main() {
      define('hello', allowInterop((req, HttpResponse res) {
        res.success('Yeah!');
      }));
    }
    

    Now, everything work. I can enjoy my sunday.