typescript - 使用 Firebase 实时数据库,在触发器中创建条目
问题描述
您好 stackoverflow 和多年来一直在帮助我的人民,这是我的第一个问题!
我在 Android 上编程已经有一段时间了,我喜欢测试优化应用程序的新方法。现在我遇到了 Firebase Relatime 数据库以及使用“功能”触发服务器端条目的可能性。我观看了数十个 Firebase 视频,浏览了 Firebase 页面,并使用 Typescript 尝试了整个过程
不幸的是,我的英语非常非常糟糕,我找不到任何关于如何让简单代码工作的例子。我用 Node.js 创建了一个代码,用 firebase 模拟器对其进行了测试:启动,它可以工作,但在“Firebase 部署”之后不在 Google 服务器上。目标是在触发入口后在另一条路径上进行入口:我仍然不明白原理。这给了我最大的困难,有人可以解释它可能是什么以及它应该是什么样子吗?
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin'
const serviceAccount = require('../serviceAccount.json')
let userId = "";
admin.initializeApp({
credential: admin.credential.applicationDefault() ,
databaseURL:`https://${serviceAccount.project_id}.firebasio.com`
});
// const db = admin.database();
// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//
export const helloWorld = functions.https.onRequest((request, response) => {
console.log("hallo world");
response.send("Hello from Firebase!");
});
export const setPlayerInTeam = functions.database.ref('{userId}/team/{teamId}/allPlayer/{spielerId}/')
.onCreate( (snapshot,context)=>{
const original = snapshot.val();
userId = context.params.userId;
console.log('userId',userId);
console.log('teamId',context.params.teamId);
console.log('spielerId',context.params.spielerId);
console.log('original.name',original.name);
console.log('Updates',context.params.spielerId," "+context.params.teamId);
const adaNameRef = admin.database().ref();
adaNameRef.child('test').push({ first: 'Ada', last: 'Lovelace' }).then(function() {
console.log('Synchronization succeeded');
})
.catch(function(error) {
console.log('Synchronization failed');
});
return adaNameRef;
});
我也试过异步,根据控制台的结果是一样的
.onCreate(async (snapshot,context)=>{
......
await adaNameRef.child('test')
我的 Powershell 控制台说:
userId TZBAxFGSgZPZBoYZz7F4MVm5MAP2
> teamId -M797y0K4BBCEOa_nVoB
> spielerId -M7skpftMOOF4QlEW2kv
> original.name luigi
> Updates -M7skpftMOOF4QlEW2kv -M797y0K4BBCEOa_nVoB
i functions: Finished "setPlayerInTeam" in ~1s
> Synchronization succeeded
我的 Firebase 控制台日志说:
Function execution started
Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions
userId TZBAxFGSgZPZBoYZz7F4MVm5MAP2
teamId -M8e9ro1Dlb7VA9Pab4t
spielerId -M8e9ozdo0uU6NCXfG2z
original.name Ayla
Updates -M8e9ozdo0uU6NCXfG2z -M8e9ro1Dlb7VA9Pab4t
Function execution took 1151 ms, finished with status: 'ok'
没有成功,没有错误?
抱歉,英语不好,所有内容都是用谷歌翻译的,我已经谢谢你了
解决方案
我为您创建了一个示例代码,这可以帮助您开始。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
//Initialize the App
admin.initializeApp(functions.config().firebase);
//When an userID is created, the function will be triggered
exports.makeUppercase = functions.database.ref('/pamyu/{userID}')
.onCreate((snapshot, context) => {
//Then I'll create an entry in this other path
//You can change the path according to your DB
admin.database().ref("/kyary").update({
userid: "1234567890"
});
});
祝你好运!:D
推荐阅读
- python - 如果找到字符串/模式,则将其从列表中删除
- amazon-web-services - 使用 cli 将触发器添加到 lambda 函数
- python - 如何使用从另一个数据框中获取的值向数据框添加新列?
- r - 检测 PDF 是否包含可搜索的文本
- keycloak - Apache Zeppelin 和 KeyCloak 集成 - 不支持的 RSASSA 算法
- mysql - SQL查询很慢。没有索引如何改进?
- tensorflow - TypeError: can't pickle _thread.RLock objects - 在 keras 中保存模型
- javascript - 在 Vue 组件渲染上同时转换多个元素
- c++ - 我应该何时覆盖 basic_streambuf::seekoff?
- xcode - 无法通过签名、规范化、非沙盒化的 Mac 应用程序创建文件