【发布时间】: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