英文:
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 更改的管理员应用实例。
此外,你需要:
- 正确返回
getIdsOfUsersWhoHaveSeenUser
函数中的 Promise 链。 - 请注意,
whereField()
不是 Admin SDK 的方法。你应该使用where()
方法,详见文档。
英文:
You should do as follows:
const functions = require("firebase-functions");
const admin = require('firebase-admin');
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:
- Correctly return the Promise chain in your
getIdsOfUsersWhoHaveSeenUser
function - Note that
whereField()
is not a method of the Admin SDK. You should use thewhere()
method, see the doc.
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);
});
};
and also take into account the fact that this getIdsOfUsersWhoHaveSeenUser
function is asynchronous.
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);
});
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论