简体   繁体   English

如何使用 Javascript 从云 Function 更新实时数据库?

[英]How to update Realtime Database from Cloud Function using Javascript?

I am creating a cloud function for my iOS app - I want it to create a Stripe customer when a user signs up (this works properly), but I can't set any data to my realtime database within the function.我正在为我的 iOS 应用程序创建一个云 function - 我希望它在用户注册时创建一个 Stripe 客户(这工作正常),但我无法在 ZC1C425268E68384F145A 中将任何数据设置到我的实时数据库中。

Here is my latest attempt这是我最近的尝试

const functions = require('firebase-functions')
const admin = require('firebase-admin')

admin.initializeApp(functions.config().firebase);
const stripe = require('stripe')(functions.config().stripe.testkey)



//when user is created: 
//1.create user with Stripe
//2.update users/user.uid/registeredStripe: "no" value to "yes"

exports.createStripeCustomer = functions.auth.user().onCreate((user) => {

  return stripe.customers.create({

    email: user.email

  }).then((customer) => {
    return admin.database().collection('users').doc(user.uid).set({registeredStripe: "yes"});
  });
});


The Stripe customer is created successfully ~ but my realtime database doesn't update at all. Stripe 客户创建成功~但我的实时数据库根本没有更新。 I've tried rewriting this line many different ways, but nothing worked.我试过用很多不同的方式重写这条线,但没有任何效果。

Edit: I am trying to update my real-time database but no update is being made (trying to change a value from “no” to “yes”. I get no errors in the log, but no update is made.编辑:我正在尝试更新我的实时数据库,但没有进行更新(尝试将值从“否”更改为“是”。我在日志中没有收到任何错误,但没有进行更新。

I suggest double checking if there are actually errors in the functions log in the Firebase console.我建议仔细检查 Firebase 控制台中的功能日志中是否确实存在错误。 I would not expect this code to work at all:我根本不希望这段代码起作用:

return admin.database().collection('users').doc(user.uid).set({registeredStripe: "yes"});

admin.database() works with Realtime Database, but "collection" and "document" are Firestore methods. admin.database()适用于实时数据库,但“收集”和“文档”是 Firestore 方法。 This code should generate an error saying that "collection" is not a method.此代码应生成一个错误,指出“集合”不是一种方法。 Firestore and Realtime Database are completely different databases with different APIs. Firestore 和实时数据库是完全不同的数据库,具有不同的 API。

If you want to update Realtime Database, you should use its own API.如果你想更新实时数据库,你应该使用它自己的 API。 You will use methods such as "child" to indicate the location in the database to change.您将使用诸如“child”之类的方法来指示要更改的数据库中的位置。 Please refer to the documentation .请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 javascript 将 firebase 云 function 中的数组发送到实时数据库 - Send an array in firebase cloud function to the realtime database using javascript 如何使用 Javascript 云从 Firebase 实时数据库中随机获取子密钥名称 function - How to get a child key name randomly from the Firebase Realtime database with a Javascript cloud function 如何使用云 function 获取 Firebase 中的实时数据库? - How to get Realtime Database in Firebase by using cloud function? 如何从云功能中止到Firebase实时数据库的创建? - How to abort creation into firebase realtime database from a cloud function? 如何使用Cloud函数中的indexOn从实时数据库检索数据 - How to retrieve data from realtime database using indexOn in cloud functions Firebase实时数据库中的云功能 - Cloud Function in Firebase Realtime Database 如何使用JavaScript从Firebase实时数据库中删除特定子项? - How to delete a specific child from firebase realtime database using javascript? 如何使用JavaScript从Firebase实时数据库中检索对象数组? - How to retrieve an array of objects from a firebase realtime database using javascript? 如何在Firebase实时数据库中使用更新值? - How to update a value using in firebase realtime database? 如何做云 function firebase 将数据从 Bucket 实时导入数据库 - how to do a cloud function firebase importing data from Bucket to database realtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM