【问题标题】:Typescript transpiling to javascript for firebase functions将 Typescript 转换为 javascript 以实现 firebase 功能
【发布时间】:2017-08-15 20:54:47
【问题描述】:

我已经开始深入研究 firebase 的云功能,但是作为一名 Android 开发人员,在这么久之后重新回到 javascript 领域已经很艰难了。

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

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

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

我已经成功编译了我的 Typescript,但是在转译为 javascript 时,它无法正确编译。

谁能帮我解决这个问题?

下面的 TS 脚本

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
            }
    });
});

而javascript编译出来的代码是

"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
        }
    });
}));

违规行是这样的

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

更具体地说是 => 字符。我得到的错误是表达式预期的。

这是我的 tsconfig.json 文件

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

如果有人对此有任何想法,我将不胜感激。

【问题讨论】:

  • 编译后的代码是有效且正确的 ES6 代码。尝试在tsconfig.json 中指定"target": "es5"。顺便说一句,哪个软件告诉你=> 是语法错误?

标签: node.js typescript firebase-realtime-database firebase-cloud-messaging google-cloud-functions


【解决方案1】:

我发现了我的问题,我在 Webstorm 5.1 中设置了旧版本的 javascript 进行编译。升级到 6 解决了这些问题。

【讨论】:

    猜你喜欢
    • 2019-06-06
    • 2020-04-02
    • 2020-04-28
    • 1970-01-01
    • 2016-11-18
    • 2022-06-11
    • 2023-02-05
    • 2021-01-31
    • 2019-05-16
    相关资源
    最近更新 更多