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

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

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

以下是我编写的代码:

const functions = require("firebase-functions");
const admin = require('firebase-admin');
const { firestore } = require("firebase-admin");

admin.initializeApp();

const db = firestore.firestore();

exports.getUsers = functions.https.onRequest((request, response) => {
  let user = request.query.user;
  let ids = getIdsOfUsersWhoHaveSeenUser(user);
  let jsonBody = {
    "users": ids
  }
  let responseBody = JSON.stringify(jsonBody);
  response.send(responseBody);
});

function getIdsOfUsersWhoHaveSeenUser(user) {
  const query = db.collection('users').where('seenUsers', 'array-contains', user);
  query.get()
    .then(snapshot => {
      const documents = snapshot.docs;
      const ids = documents.map(doc => doc.id);
      return ids;
    })
    .catch (error => {
      console.log(error);
    });
};

我还尝试过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

const functions = require("firebase-functions");
const admin = require('firebase-admin');
const { firestore } = require("firebase-admin");

admin.initializeApp();

const db = firestore.firestore();


exports.getUsers = functions.https.onRequest((request, response) => {
  let user = request.query.user;
  let ids = getIdsOfUsersWhoHaveSeenUser(user);
  let jsonBody = {
    "users": ids
  }
  let responseBody = JSON.stringify(jsonBody);
  response.send(responseBody);
});

function getIdsOfUsersWhoHaveSeenUser(user) {
  const query = db.collection('users').whereField('seenUsers', arrayContains(user));
  query.get()
    .then(snapshot => {
      const documents = snapshot.docs;
      const ids = documents.map(doc => doc.id);
      return ids;
    })
    .catch (error => {
      console.log(error);
    });
};

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

答案1

得分: 1

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

admin.initializeApp();

const db = admin.firestore();

function getIdsOfUsersWhoHaveSeenUser(user) {
  const query = db.collection('users').where("seenUsers", "array-contains", user);  // <== See change here

  return query.get()   // <== See return here
    .then(snapshot => {
      const documents = snapshot.docs;
      const ids = documents.map(doc => doc.id);
      return ids;
    })
    .catch (error => {
      console.log(error);
    });
};

exports.getUsers = functions.https.onRequest((request, response) => {
  let user = request.query.user;
  getIdsOfUsersWhoHaveSeenUser(user)
   .then(ids => {
      let jsonBody = {
         "users": ids
      }
      let responseBody = JSON.stringify(jsonBody);
      response.send(responseBody);

    });
});

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

此外,你需要:

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

You should do as follows:

const functions = require(&quot;firebase-functions&quot;);
const admin = require(&#39;firebase-admin&#39;);

admin.initializeApp();

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.

function getIdsOfUsersWhoHaveSeenUser(user) {
  const query = db.collection(&#39;users&#39;).where(&quot;seenUsers&quot;, &quot;array-contains&quot;, &quot;user&quot;);  // &lt;== See change here

  return query.get()   // &lt;== See return here
    .then(snapshot =&gt; {
      const documents = snapshot.docs;
      const ids = documents.map(doc =&gt; doc.id);
      return ids;
    })
    .catch (error =&gt; {
      console.log(error);
    });
};

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

exports.getUsers = functions.https.onRequest((request, response) =&gt; {
  let user = request.query.user;
  getIdsOfUsersWhoHaveSeenUser(user)
 .then(ids =&gt; {
    let jsonBody = {
       &quot;users&quot;: ids
    }
    let responseBody = JSON.stringify(jsonBody);
    response.send(responseBody);

  });
});

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:

确定