[英]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.