将firebase函数转换为JavaScript的Typescript [英] Typescript transpiling to javascript for firebase functions

查看:221
本文介绍了将firebase函数转换为JavaScript的Typescript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始深入研究firebase的云端功能,但是作为一个Android开发者,在经过这么长时间之后,已经变成了javascript。

我已经配置了一个新的使用firebase命令行工具,例如firebase login / init / deploy。还有npm和所有需要的依赖。

我正在开发使用Webstorm,并决定尝试按照youtube上的视频来使用Typescript。



[


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.

解决方案

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.

这篇关于将firebase函数转换为JavaScript的Typescript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆