简体   繁体   English

在本地功能 firebase 仿真器中使用实时/在线 Firestore

[英]Use live/online firestore in local Functions firebase emulator

I am making a cloud function and using the firebase local emulator.我正在制作云 function 并使用 firebase 本地仿真器。 Node.js Node.js

I follow this guide where it uses the firestore on the local machine.我按照本指南使用本地计算机上的 firestore。 But i want to use my firestore that already is online.但我想使用我已经在线的 Firestore。 Is there any way to achieve this?有什么办法可以做到这一点?

I understand that you want to test your function within local emulator, but use cloud database.我了解您想在本地模拟器中测试您的 function,但使用云数据库。

This is possible, although I don't think it very helpful.这是可能的,虽然我不认为它很有帮助。 You can test easier deploying your function to the cloud.您可以测试更轻松地将 function 部署到云。 Anyway....反正....

When you test both in emulator and you have both on cloud it's enough to initiate like this (its from here ):当您在模拟器中测试两者并且您都在云上时,这样启动就足够了(从这里开始):

const admin = require('firebase-admin');
admin.initializeApp();

const db = admin.firestore();

However if you want to connect from local to cloud you need to initialize like "Initialize on your own server" from the same doc.但是,如果您想从本地连接到云,则需要从同一个文档中初始化,例如“在您自己的服务器上初始化”。

I have created similar example from helloworld function working on my side (showing all fields of the docs).我从 helloworld function 中创建了类似的示例(显示文档的所有字段)。 I have collection "users" and document "1" in my firestore:我的firestore中有集合“用户”和文档“1”:

const functions = require("firebase-functions");
const admin = require('firebase-admin');
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.helloWorld = functions.https.onRequest((request, response) => {   
  
       const serviceAccount = require('./serviceAccountKey.json');
       admin.initializeApp({
               credential: admin.credential.cert(serviceAccount)
       });
       const db = admin.firestore();
       const ref = db.collection('users').doc('1');
       ref.get().then((doc)=> {
               functions.logger.info(doc, {structuredData: true});
               response.send('Document data:'+JSON.stringify(doc.data()))
       }).catch(e => {
               console.log('error; '+e)
       })
});

The serviceAccountKey.json you can easily generate in Firebase console in Project settins/Service Accounts tab.您可以在 Firebase 控制台的项目设置/服务帐户选项卡中轻松生成serviceAccountKey.json Just remember that if you want to deploy to cloud you should change the app initialization.请记住,如果您想部署到云,您应该更改应用程序初始化。

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

相关问题 如何在Node.js中使用Firestore本地模拟器? - How to use firestore local emulator in Node.js? 我可以将 Firebase Functions Emulator 与 GCP Cloud Functions 一起使用吗 - Can I use Firebase Functions Emulator with GCP Cloud Functions 同时连接到 Firestore 在线和模拟器? - Connect to both Firestore online and emulator at the same time? Firebase云功能/ Firestore - Firebase Cloud Functions / Firestore 如何设置用于单元测试的测试套件 Firebase 与模拟器连接的功能,特别是 Firestore 模拟器? - How to setup a test suit for unit testing Firebase Functions that connect with emulators, specifically the Firestore emulator? 如何通过新的模块化 Firestore API 使用 Firebase Cloud Functions? - How to use Firebase Cloud Functions with the new modular Firestore API? 如何在 netlify lamda 中使用 firebase 云函数的 firestore.onWrite() - How to use firebase cloud functions' firestore.onWrite() in netlify lamda 我想通过使用 node js 函数更新 firebase firestore 中的 hashmap - i want to update hashmap in firebase firestore by the use of functions node js Firebase服务-仅功能VS本地仿真器以在本地运行云功能吗? - Firebase serve --only functions VS local emulator to run cloud functions locally? Firebase Firestore 模拟器如何工作? - How does Firebase Firestore emulator work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM