我正在尝试从可调用的Google Cloud Function返回QuerySnapshot
。
下面是细节,但我的核心问题是,我应该期望能够做到这一点还是这是一个错误?
import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';
import * as firestore from '@google-cloud/firestore';
export const callableTest = functions.https.onCall(
async (data: Map<string, any>, context: functions.https.CallableContext):
Promise<firestore.QuerySnapshot> => {
const db = admin.firestore();
return db.collection('test')
.get();
// I also tried this
//return db.collection('test').get().then(querySnapshot => {
// return querySnapshot;
//});
});
test
集合可以包含任意数量的文档。在这种情况下,有两个文档。
这是我的客户端HTML页面,但我也在Flutter和Node中尝试了同样的结果。
<!DOCTYPE html>
<html>
<head>
<script src="https://www.gstatic.com/firebasejs/5.7.3/firebase.js"></script>
<script>
// Initialize Firebase
var config = {
apiKey: "### API KEY ###",
authDomain: "<project-id>.firebaseapp.com",
databaseURL: "https://<project-id>.firebaseio.com",
projectId: "<project-id>",
storageBucket: "<project-id>.appspot.com",
messagingSenderId: "<sender-id>"
};
firebase.initializeApp(config);
var testClientCall = firebase.functions().httpsCallable('callableTest');
testClientCall ({}).then(function(result) {
// Read result of the Cloud Function.
console.log("Cloud Function result:");
console.log(result);
});
</script>
</head>
<body></body>
</html>
这是返回的结果,它看起来像QuerySnapshot
,但不包含任何DocumentSnapshots
(_docs
属性),即使{{ 1}}属性指示应该有两个文档。
_size
起初,我认为序列化可能是一个问题,但是如果我修改云函数以仅返回单个{"data":{"_query":{"_firestore":{"_clientPool":{"concurrentOperationLimit":100,"clientFactory":{},"activeClients":{}},"_settings":{"projectId":"<project-id>","firebaseVersion":"6.4.0","libName":"gccl","libVersion":"0.19.0 fire/6.4.0"},"_settingsFrozen":false,"_clientInitialized":{"domain":{"domain":null,"_events":{"error":{}},"_eventsCount":1,"_maxListeners":null,"members":[]}},"_serializer":{"createReference":{},"timestampsInSnapshots":false},"_referencePath":{"segments":[],"projectId":"project-id","databaseId":"(default)"},"_lastSuccessfulRequest":1547738645649,"_validator":{"isFunction":{},"isOptionalFunction":{},"isInteger":{},"isOptionalInteger":{},"isNumber":{},"isOptionalNumber":{},"isObject":{},"isOptionalObject":{},"isString":{},"isOptionalString":{},"isBoolean":{},"isOptionalBoolean":{},"isArrayElement":{},"isOptionalArrayElement":{},"isDeletePrecondition":{},"isOptionalDeletePrecondition":{},"isDocument":{},"isOptionalDocument":{},"isDocumentReference":{},"isOptionalDocumentReference":{},"isFieldPath":{},"isOptionalFieldPath":{},"isFieldValue":{},"isOptionalFieldValue":{},"isFieldOrder":{},"isOptionalFieldOrder":{},"isQueryComparison":{},"isOptionalQueryComparison":{},"isQueryValue":{},"isOptionalQueryValue":{},"isResourcePath":{},"isOptionalResourcePath":{},"isSetOptions":{},"isOptionalSetOptions":{},"isReadOptions":{},"isOptionalReadOptions":{},"isUpdateMap":{},"isOptionalUpdateMap":{},"isUpdatePrecondition":{},"isOptionalUpdatePrecondition":{}},"_preferTransactions":true},"_path":{"segments":["test"],"projectId":"project-id","databaseId":"(default)"},"_fieldFilters":[],"_fieldOrders":[],"_queryOptions":{},"_validator":{"isFunction":{},"isOptionalFunction":{},"isInteger":{},"isOptionalInteger":{},"isNumber":{},"isOptionalNumber":{},"isObject":{},"isOptionalObject":{},"isString":{},"isOptionalString":{},"isBoolean":{},"isOptionalBoolean":{},"isArrayElement":{},"isOptionalArrayElement":{},"isDeletePrecondition":{},"isOptionalDeletePrecondition":{},"isDocument":{},"isOptionalDocument":{},"isDocumentReference":{},"isOptionalDocumentReference":{},"isFieldPath":{},"isOptionalFieldPath":{},"isFieldValue":{},"isOptionalFieldValue":{},"isFieldOrder":{},"isOptionalFieldOrder":{},"isQueryComparison":{},"isOptionalQueryComparison":{},"isQueryValue":{},"isOptionalQueryValue":{},"isResourcePath":{},"isOptionalResourcePath":{},"isSetOptions":{},"isOptionalSetOptions":{},"isReadOptions":{},"isOptionalReadOptions":{},"isUpdateMap":{},"isOptionalUpdateMap":{},"isUpdatePrecondition":{},"isOptionalUpdatePrecondition":{}},"_serializer":{"createReference":{},"timestampsInSnapshots":false}},"_readTime":{"_seconds":1547738645,"_nanoseconds":638303000},"_size":2,"_materializedDocs":null,"_materializedChanges":null,"_docs":{},"_changes":{},"_validator":{"isFunction":{},"isOptionalFunction":{},"isInteger":{},"isOptionalInteger":{},"isNumber":{},"isOptionalNumber":{},"isObject":{},"isOptionalObject":{},"isString":{},"isOptionalString":{},"isBoolean":{},"isOptionalBoolean":{},"isArrayElement":{},"isOptionalArrayElement":{},"isDeletePrecondition":{},"isOptionalDeletePrecondition":{},"isDocument":{},"isOptionalDocument":{},"isDocumentReference":{},"isOptionalDocumentReference":{},"isFieldPath":{},"isOptionalFieldPath":{},"isFieldValue":{},"isOptionalFieldValue":{},"isFieldOrder":{},"isOptionalFieldOrder":{},"isQueryComparison":{},"isOptionalQueryComparison":{},"isQueryValue":{},"isOptionalQueryValue":{},"isResourcePath":{},"isOptionalResourcePath":{},"isSetOptions":{},"isOptionalSetOptions":{},"isReadOptions":{},"isOptionalReadOptions":{},"isUpdateMap":{},"isOptionalUpdateMap":{},"isUpdatePrecondition":{},"isOptionalUpdatePrecondition":{}}}}
,则会返回预期的数据。
答案 0 :(得分:1)
这是序列化的问题。您正在尝试返回一个不打算直接序列化的复杂对象。相反,您应该迭代查询快照中的文档,构建要返回的任何对象(数组?),然后返回该对象。像这样:
const querySnapshot = await db.collection('test').get();
return querySnapshot.docs.map(doc => doc.data());