node.js - 将 Typescript 转换为 javascript 以实现 Firebase 功能

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

我已经开始深入研究 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"
  ]
}

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

最佳答案

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

enter image description here

关于node.js - 将 Typescript 转换为 javascript 以实现 Firebase 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42973741/

相关文章:

android - 字段 'Signature' 的值不正确 : value is 662655099 but 67324752 expected

Java 代码未运行到下一个 Intent ,但在调试时有效

swift - 从帖子 ID firebase 和 swift 获取帖子

java - 从 Node.js 中运行的 jar 调用 java 方法的最佳方法是什么?

node.js - 在 nodejs 中使用 Apache Thrift 进行序列化-反序列化

javascript - 在 TypeScript 中包装 Firebase promise

angular - ng6 : Dynamically form Variable names in html

node.js - 错误找不到模块 'sequelize'

node.js - Firestore set doc with merge option true or update doc

javascript - 普通对象 VS 模型对象的类实例