Firebase数据库在HTTPS Firebase函数上未定义。

huangapple go评论123阅读模式
英文:

Firebase db is not defined on HTTPS Firebase Function

问题

我正在创建一个Firebase HTTPS函数,用于从Firestore读取数据并返回给用户。

然而,我遇到了'db is not defined'的问题。我尝试了不同的编写方式,比如const db = firebase.firestore();,但这会导致错误TypeError: firestore.firestore is not a function

以下是我编写的代码:

  1. const functions = require("firebase-functions");
  2. const admin = require('firebase-admin');
  3. const { firestore } = require("firebase-admin");
  4. admin.initializeApp();
  5. const db = firestore.firestore();
  6. exports.getUsers = functions.https.onRequest((request, response) => {
  7. let user = request.query.user;
  8. let ids = getIdsOfUsersWhoHaveSeenUser(user);
  9. let jsonBody = {
  10. "users": ids
  11. }
  12. let responseBody = JSON.stringify(jsonBody);
  13. response.send(responseBody);
  14. });
  15. function getIdsOfUsersWhoHaveSeenUser(user) {
  16. const query = db.collection('users').where('seenUsers', 'array-contains', user);
  17. query.get()
  18. .then(snapshot => {
  19. const documents = snapshot.docs;
  20. const ids = documents.map(doc => doc.id);
  21. return ids;
  22. })
  23. .catch (error => {
  24. console.log(error);
  25. });
  26. };

我还尝试过admin.firestore().collection(),但那也不起作用。

英文:

I am creating a Firebase HTTPS function that reads data from Firestore and returns to the user.

However, I get 'db is not defined'. I've tried different ways of writing this such as const db = firebase.firestore(); but this causes an error TypeError: firestore.firestore is not a function

Here is the code I've written

  1. const functions = require("firebase-functions");
  2. const admin = require('firebase-admin');
  3. const { firestore } = require("firebase-admin");
  4. admin.initializeApp();
  5. const db = firestore.firestore();
  6. exports.getUsers = functions.https.onRequest((request, response) => {
  7. let user = request.query.user;
  8. let ids = getIdsOfUsersWhoHaveSeenUser(user);
  9. let jsonBody = {
  10. "users": ids
  11. }
  12. let responseBody = JSON.stringify(jsonBody);
  13. response.send(responseBody);
  14. });
  15. function getIdsOfUsersWhoHaveSeenUser(user) {
  16. const query = db.collection('users').whereField('seenUsers', arrayContains(user));
  17. query.get()
  18. .then(snapshot => {
  19. const documents = snapshot.docs;
  20. const ids = documents.map(doc => doc.id);
  21. return ids;
  22. })
  23. .catch (error => {
  24. console.log(error);
  25. });
  26. };

I've also tried admin.firestore().collection() but that doesn't work either.

答案1

得分: 1

  1. const functions = require("firebase-functions");
  2. const admin = require('firebase-admin');
  3. admin.initializeApp();
  4. const db = admin.firestore();
  5. function getIdsOfUsersWhoHaveSeenUser(user) {
  6. const query = db.collection('users').where("seenUsers", "array-contains", user); // <== See change here
  7. return query.get() // <== See return here
  8. .then(snapshot => {
  9. const documents = snapshot.docs;
  10. const ids = documents.map(doc => doc.id);
  11. return ids;
  12. })
  13. .catch (error => {
  14. console.log(error);
  15. });
  16. };
  17. exports.getUsers = functions.https.onRequest((request, response) => {
  18. let user = request.query.user;
  19. getIdsOfUsersWhoHaveSeenUser(user)
  20. .then(ids => {
  21. let jsonBody = {
  22. "users": ids
  23. }
  24. let responseBody = JSON.stringify(jsonBody);
  25. response.send(responseBody);
  26. });
  27. });

admin 是可以进行 Cloud Firestore 更改的管理员应用实例。

此外,你需要:

  1. 正确返回 getIdsOfUsersWhoHaveSeenUser 函数中的 Promise 链。
  2. 请注意,whereField() 不是 Admin SDK 的方法。你应该使用 where() 方法,详见文档
英文:

You should do as follows:

  1. const functions = require(&quot;firebase-functions&quot;);
  2. const admin = require(&#39;firebase-admin&#39;);
  3. admin.initializeApp();
  4. const db = admin.firestore();

admin is the admin app instance from which Cloud Firestore changes can be made.


In addition you need to:

  1. Correctly return the Promise chain in your getIdsOfUsersWhoHaveSeenUser function
  2. Note that whereField() is not a method of the Admin SDK. You should use the where() method, see the doc.

  1. function getIdsOfUsersWhoHaveSeenUser(user) {
  2. const query = db.collection(&#39;users&#39;).where(&quot;seenUsers&quot;, &quot;array-contains&quot;, &quot;user&quot;); // &lt;== See change here
  3. return query.get() // &lt;== See return here
  4. .then(snapshot =&gt; {
  5. const documents = snapshot.docs;
  6. const ids = documents.map(doc =&gt; doc.id);
  7. return ids;
  8. })
  9. .catch (error =&gt; {
  10. console.log(error);
  11. });
  12. };

and also take into account the fact that this getIdsOfUsersWhoHaveSeenUser function is asynchronous.

  1. exports.getUsers = functions.https.onRequest((request, response) =&gt; {
  2. let user = request.query.user;
  3. getIdsOfUsersWhoHaveSeenUser(user)
  4. .then(ids =&gt; {
  5. let jsonBody = {
  6. &quot;users&quot;: ids
  7. }
  8. let responseBody = JSON.stringify(jsonBody);
  9. response.send(responseBody);
  10. });
  11. });

huangapple
  • 本文由 发表于 2023年1月9日 14:54:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定