Typescript transpiling to javascript for firebase

2019-07-27 12:01发布

问题:

I have started to delve into cloud functions for firebase, but being an Android dev moving back into javascript after so long has been tough.

I have configured a new project with the firebase command line tools i.e. firebase login/init/deploy. And also npm and all the dependencies needed.

I am developing using Webstorm and decided to try to use Typescript as per the video on youtube.

[https://www.youtube.com/watch?v=GNR9El3XWYo&t=1106s][1]

I've managed to get my Typescript compiling, but when transpiling to javascript it doesn't compile correctly.

Can anyone help me with this issue?

TS script below

import * as functions from 'firebase-functions'
import * as request from 'request-promise'

export let addPlayerRequestNotification = functions.database
    .ref('notifications/{id}/notification')
    .onWrite(async event => {
    let notificaion = event.data.val();

    let oauthToken = await functions.config().firebase.credential.getAccessToken();

    await request ({
            url: 'https://fcm.googleapis.com/fcm/send',
            method: 'POST',
            headers: {
                    'Content-Type' :' application/json',
                    'Authorization': 'key='+oauthToken
            },
            body: {
                "notification": {
                    "title": notificaion.title,
                    "text": notificaion.text,
                },
                to : '/topics/'+notificaion.topic
            }
    });
});

And the javascript compiled code is

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const functions = require("firebase-functions");
const request = require("request-promise");
exports.addPlayerRequestNotification = functions.database
    .ref('notifications/{id}/notification')
    .onWrite((event) => __awaiter(this, void 0, void 0, function* () {
    let notificaion = event.data.val();
    let oauthToken = yield functions.config().firebase.credential.getAccessToken();
    yield request({
        url: 'https://fcm.googleapis.com/fcm/send',
        method: 'POST',
        headers: {
            'Content-Type': ' application/json',
            'Authorization': 'key=' + oauthToken
        },
        body: {
            "notification": {
                "title": notificaion.title,
                "text": notificaion.text,
            },
            to: '/topics/' + notificaion.topic
        }
    });
}));

The offending line is this

.onWrite((event) => __awaiter(this, void 0, void 0, function* () {

More specifically the => character. The error I get is expression expected.

This is my tsconfig.json file

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs"
  },
  "include": [
    "functions/**/*.ts"
  ],
  "exclude": [
    "functions/node_modules"
  ]
}

If anyone has any ideas on this, I would greatly appreciate any help.

回答1:

I found my problem, I had set an old version of javascript to compile against in Webstorm 5.1. Upgrading to 6 fixed the issues.