I am using dart2js to create firebase functions. Within such a function I have to invoke a http request for some web apis (in this example a simple web-site request to www.google.com :-)), but get "ReferenceError: XMLHttpRequest is not defined". Running the same code in a Dart app on Android or iOS is working fine. I am using https://pub.dev/packages/http.
import 'package:http/http.dart' as http;
class ExecuteHttpRequest {
Future<void> execute() async {
Uri getUri = Uri.https('www.google.com', '');
final response = await http.get(getUri);
if (response.statusCode == 200) print('success');
}
}
the class is e.g. wrapped in a simple firebase function
import 'package:node_io/node_io.dart';
import 'package:firebase_functions_interop/firebase_functions_interop.dart';
import 'package:nodejs_test/ExecuteHttpRequest.dart';
void main() {
functions['executeHttpRequest'] = functions.https.onRequest((ExpressHttpRequest request) async {
await ExecuteHttpRequest().execute();
request.response
..headers.contentType = ContentType.text
..write("success :-)")
..close();
});
}
when building and executing, I get the following:
% pub run build_runner build -o node:build
[INFO] Generating build script completed, took 301ms
[WARNING] The package `nodejs_test` does not include some required sources in any of its targets (see their build.yaml file).
The missing sources are:
- $package$
[INFO] Reading cached asset graph completed, took 178ms
[INFO] Checking for updates since last build completed, took 611ms
[INFO] Running build completed, took 184ms
[INFO] Caching finalized dependency graph completed, took 117ms
[INFO] Reading manifest at build/.build.manifest completed, took 0ms
[INFO] Deleting previous outputs in `build` completed, took 144ms
[INFO] Creating merged output dir `build` completed, took 614ms
[INFO] Writing asset manifest completed, took 3ms
[INFO] Succeeded after 1.1s with 0 outputs (0 actions)
% firebase emulators:start
i emulators: Starting emulators: functions, firestore, database, hosting
⚠ functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: auth, pubsub
⚠ Your requested "node" version "10" doesn't match your global version "14"
i firestore: Firestore Emulator logging to firestore-debug.log
i database: Database Emulator logging to database-debug.log
⚠ hosting: Authentication error when trying to fetch your current web app configuration, have you run firebase login?
⚠ hosting: Could not fetch web app configuration and there is no cached configuration on this machine. Check your internet connection and make sure you are authenticated. To continue, you must call firebase.initializeApp({...}) in your code before using Firebase.
i hosting[nodejs-test]: Serving hosting files from: public
✔ hosting[nodejs-test]: Local server: http://localhost:5000
i ui: Emulator UI logging to ui-debug.log
i functions: Watching "/Users/stefano/development/nodejs-test/functions" for Cloud Functions...
✔ functions[executeHttpRequest]: http function initialized (http://localhost:5001/nodejs-test/us-central1/executeHttpRequest).
┌─────────────────────────────────────────────────────────────┐
│ ✔ All emulators ready! It is now safe to connect your app. │
│ i View Emulator UI at http://localhost:4000 │
└─────────────────────────────────────────────────────────────┘
┌───────────┬────────────────┬─────────────────────────────────┐
│ Emulator │ Host:Port │ View in Emulator UI │
├───────────┼────────────────┼─────────────────────────────────┤
│ Functions │ localhost:5001 │ http://localhost:4000/functions │
├───────────┼────────────────┼─────────────────────────────────┤
│ Firestore │ localhost:8080 │ http://localhost:4000/firestore │
├───────────┼────────────────┼─────────────────────────────────┤
│ Database │ localhost:9000 │ http://localhost:4000/database │
├───────────┼────────────────┼─────────────────────────────────┤
│ Hosting │ localhost:5000 │ n/a │
└───────────┴────────────────┴─────────────────────────────────┘
Emulator Hub running at localhost:4400
Other reserved ports: 4500
Issues? Report them at https://github.com/firebase/firebase-tools/issues and attach the *-debug.log files.
i functions: Beginning execution of "executeHttpRequest"
> /Users/stefano/development/nodejs-test/functions/build/index.dart.js:8378
> throw error;
> ^
>
> ReferenceError: XMLHttpRequest is not defined
> at /Users/stefano/development/nodejs-test/functions/build/index.dart.js:10055:15
> at _wrapJsFunctionForAsync_closure.$protected (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:3431:15)
> at _wrapJsFunctionForAsync_closure.call$2 (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:7301:12)
> at _awaitOnObject_closure.call$1 (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:7287:32)
> at _RootZone.runUnary$2$2 (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:8450:18)
> at _Future__propagateToListeners_handleValueCallback.call$0 (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:7689:61)
> at Object._Future__propagateToListeners (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:3530:104)
> at _Future._completeWithValue$1 (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:7502:9)
> at _Future__asyncComplete_closure.call$0 (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:7604:10)
> at Object._microtaskLoop (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:3593:21) {
> dartException: <ref *1> ReferenceError: XMLHttpRequest is not defined
> at /Users/stefano/development/nodejs-test/functions/build/index.dart.js:10055:15
> at _wrapJsFunctionForAsync_closure.$protected (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:3431:15)
> at _wrapJsFunctionForAsync_closure.call$2 (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:7301:12)
> at _awaitOnObject_closure.call$1 (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:7287:32)
> at _RootZone.runUnary$2$2 (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:8450:18)
> at _Future__propagateToListeners_handleValueCallback.call$0 (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:7689:61)
> at Object._Future__propagateToListeners (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:3530:104)
> at _Future._completeWithValue$1 (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:7502:9)
> at _Future__asyncComplete_closure.call$0 (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:7604:10)
> at Object._microtaskLoop (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:3593:21) {
> '$cachedTrace': _StackTrace {
> _exception: [Circular *1],
> _trace: 'ReferenceError: XMLHttpRequest is not defined\n' +
> ' at /Users/stefano/development/nodejs-test/functions/build/index.dart.js:10055:15\n' +
> ' at _wrapJsFunctionForAsync_closure.$protected (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:3431:15)\n' +
> ' at _wrapJsFunctionForAsync_closure.call$2 (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:7301:12)\n' +
> ' at _awaitOnObject_closure.call$1 (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:7287:32)\n' +
> ' at _RootZone.runUnary$2$2 (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:8450:18)\n' +
> ' at _Future__propagateToListeners_handleValueCallback.call$0 (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:7689:61)\n' +
> ' at Object._Future__propagateToListeners (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:3530:104)\n' +
> ' at _Future._completeWithValue$1 (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:7502:9)\n' +
> ' at _Future__asyncComplete_closure.call$0 (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:7604:10)\n' +
> ' at Object._microtaskLoop (/Users/stefano/development/nodejs-test/functions/build/index.dart.js:3593:21)'
> }
> }
> }
now I solved the problem myself :-)
You have to use
instead of