如何使功能与Firestore一起使用? [英] How to make functions work with Firestore?
本文介绍了如何使功能与Firestore一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
functions/index.js:
functions/index.js:
const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)
const db = admin.database().ref()
exports.app = functions.https.onRequest((req, res) => {
// do whatever here...
db.child('something').once('value').then(snap => console.log(snap.key))
})
以上适用于Firebase.
Above works for Firebase.
我对使此示例有效从功能.像这样但可以正常工作的东西:
I'm interested in making this example work from functions. Something like this but working:
const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)
const db = admin.database().ref()
exports.request = functions.https.onRequest((req, res) => {
let docRef = db.collection("cities").doc("SF")
docRef.get().then(doc => {
if (doc.exists) console.log("Document data:", doc.data())
else console.log("No such document!")
}).catch(error => {
console.log("Error getting document:", error)
})
})
推荐答案
使用 admin.firestore()
代替 admin.database()
.确保您的firebase-admin模块为5.4.1或更高版本.
Use admin.firestore()
instead of admin.database()
. Be sure your firebase-admin module is 5.4.1 or better.
exports.fun = functions.https.onRequest((req, res) => {
const store = admin.firestore()
store.collection('users').doc('foo').get().then(doc => {
if (doc.exists) {
console.log(doc.data())
res.send(doc.data())
}
else {
res.send("Nothing")
}
}).catch(reason => {
console.log(reason)
res.send(reason)
})
})
这篇关于如何使功能与Firestore一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文