Javascript 无法通过API调用发送Firebase QueryDocumentSnapshot
我目前正在服务器端进行一系列分页调用中的第一次网络调用。在此之前,我在客户端进行所有调用,并将集合调用中的最后一个文档存储为偏移量 然后将偏移量作为同一集合的.startAfter调用发送。抵销凭证如下所示:Javascript 无法通过API调用发送Firebase QueryDocumentSnapshot,javascript,node.js,firebase,Javascript,Node.js,Firebase,我目前正在服务器端进行一系列分页调用中的第一次网络调用。在此之前,我在客户端进行所有调用,并将集合调用中的最后一个文档存储为偏移量 然后将偏移量作为同一集合的.startAfter调用发送。抵销凭证如下所示: exists: (...) id: (...) metadata: (...) ref: (...) _document: Document {key: DocumentKey, version: SnapshotVersion, data: ObjectValue, proto: {…}
exists: (...)
id: (...)
metadata: (...)
ref: (...)
_document: Document {key: DocumentKey, version: SnapshotVersion, data: ObjectValue, proto: {…}, hasLocalMutations: false, …}
_firestore: Firestore {_queue: AsyncQueue, INTERNAL: {…}, _config: FirestoreConfig, _databaseId: DatabaseId, _dataConverter: UserDataConverter, …}
_fromCache: false
_hasPendingWrites: false
_key: DocumentKey {path: ResourcePath}
__proto__: DocumentSnapshot
res.json(offset)
feedData = await dataWithOffset.json();
{ _ref:
{ _firestore:
{ _settings: [Object],
_settingsFrozen: true,
_serializer: [Object],
_projectId: '***-prod',
_lastSuccessfulRequest: 1566308918946,
_preferTransactions: false,
_clientPool: [Object] },
_path:
{ segments: [Array],
projectId: '***-prod',
databaseId: '(default)' } },
_fieldsProto:
{ lastModified: { timestampValue: [Object], valueType: 'timestampValue' },
...,
_serializer: { timestampsInSnapshots: true },
_readTime: { _seconds: 1566308918, _nanoseconds: 909566000 },
_createTime: { _seconds: 1565994031, _nanoseconds: 304997000 },
_updateTime: { _seconds: 1565994031, _nanoseconds: 304997000 } }
[
{
"name": "car 1",
"hp": 5
},
{
"name": "car 2",
"hp": 10
},
{
"name": "car 2.5",
"hp": 10
},
{
"name": "car 3",
"hp": 15
}
]
当我在服务器上进行调用时,我目前可以看到文档看起来是一样的,但是当我通过网络发送文档时,它似乎被剥离了,或者至少在通过JSON发送并解析回来时看起来非常不同
发送方式如下:
exists: (...)
id: (...)
metadata: (...)
ref: (...)
_document: Document {key: DocumentKey, version: SnapshotVersion, data: ObjectValue, proto: {…}, hasLocalMutations: false, …}
_firestore: Firestore {_queue: AsyncQueue, INTERNAL: {…}, _config: FirestoreConfig, _databaseId: DatabaseId, _dataConverter: UserDataConverter, …}
_fromCache: false
_hasPendingWrites: false
_key: DocumentKey {path: ResourcePath}
__proto__: DocumentSnapshot
res.json(offset)
feedData = await dataWithOffset.json();
{ _ref:
{ _firestore:
{ _settings: [Object],
_settingsFrozen: true,
_serializer: [Object],
_projectId: '***-prod',
_lastSuccessfulRequest: 1566308918946,
_preferTransactions: false,
_clientPool: [Object] },
_path:
{ segments: [Array],
projectId: '***-prod',
databaseId: '(default)' } },
_fieldsProto:
{ lastModified: { timestampValue: [Object], valueType: 'timestampValue' },
...,
_serializer: { timestampsInSnapshots: true },
_readTime: { _seconds: 1566308918, _nanoseconds: 909566000 },
_createTime: { _seconds: 1565994031, _nanoseconds: 304997000 },
_updateTime: { _seconds: 1565994031, _nanoseconds: 304997000 } }
[
{
"name": "car 1",
"hp": 5
},
{
"name": "car 2",
"hp": 10
},
{
"name": "car 2.5",
"hp": 10
},
{
"name": "car 3",
"hp": 15
}
]
然后解析如下:
exists: (...)
id: (...)
metadata: (...)
ref: (...)
_document: Document {key: DocumentKey, version: SnapshotVersion, data: ObjectValue, proto: {…}, hasLocalMutations: false, …}
_firestore: Firestore {_queue: AsyncQueue, INTERNAL: {…}, _config: FirestoreConfig, _databaseId: DatabaseId, _dataConverter: UserDataConverter, …}
_fromCache: false
_hasPendingWrites: false
_key: DocumentKey {path: ResourcePath}
__proto__: DocumentSnapshot
res.json(offset)
feedData = await dataWithOffset.json();
{ _ref:
{ _firestore:
{ _settings: [Object],
_settingsFrozen: true,
_serializer: [Object],
_projectId: '***-prod',
_lastSuccessfulRequest: 1566308918946,
_preferTransactions: false,
_clientPool: [Object] },
_path:
{ segments: [Array],
projectId: '***-prod',
databaseId: '(default)' } },
_fieldsProto:
{ lastModified: { timestampValue: [Object], valueType: 'timestampValue' },
...,
_serializer: { timestampsInSnapshots: true },
_readTime: { _seconds: 1566308918, _nanoseconds: 909566000 },
_createTime: { _seconds: 1565994031, _nanoseconds: 304997000 },
_updateTime: { _seconds: 1565994031, _nanoseconds: 304997000 } }
[
{
"name": "car 1",
"hp": 5
},
{
"name": "car 2",
"hp": 10
},
{
"name": "car 2.5",
"hp": 10
},
{
"name": "car 3",
"hp": 15
}
]
解析之后,它看起来如下所示:
exists: (...)
id: (...)
metadata: (...)
ref: (...)
_document: Document {key: DocumentKey, version: SnapshotVersion, data: ObjectValue, proto: {…}, hasLocalMutations: false, …}
_firestore: Firestore {_queue: AsyncQueue, INTERNAL: {…}, _config: FirestoreConfig, _databaseId: DatabaseId, _dataConverter: UserDataConverter, …}
_fromCache: false
_hasPendingWrites: false
_key: DocumentKey {path: ResourcePath}
__proto__: DocumentSnapshot
res.json(offset)
feedData = await dataWithOffset.json();
{ _ref:
{ _firestore:
{ _settings: [Object],
_settingsFrozen: true,
_serializer: [Object],
_projectId: '***-prod',
_lastSuccessfulRequest: 1566308918946,
_preferTransactions: false,
_clientPool: [Object] },
_path:
{ segments: [Array],
projectId: '***-prod',
databaseId: '(default)' } },
_fieldsProto:
{ lastModified: { timestampValue: [Object], valueType: 'timestampValue' },
...,
_serializer: { timestampsInSnapshots: true },
_readTime: { _seconds: 1566308918, _nanoseconds: 909566000 },
_createTime: { _seconds: 1565994031, _nanoseconds: 304997000 },
_updateTime: { _seconds: 1565994031, _nanoseconds: 304997000 } }
[
{
"name": "car 1",
"hp": 5
},
{
"name": "car 2",
"hp": 10
},
{
"name": "car 2.5",
"hp": 10
},
{
"name": "car 3",
"hp": 15
}
]
知道它为什么会失去形状吗?我能做些什么来修复它,使它恢复正常的偏移量?我是否应该转换为JSON并返回,因为这可能会剥夺一些重要的东西?解决方案测试
所以我继续做了一个沙箱来测试实现,就像我在评论中提到的那样
考虑到我创建了一个名为cars的集合,其中有4条记录如下:
exists: (...)
id: (...)
metadata: (...)
ref: (...)
_document: Document {key: DocumentKey, version: SnapshotVersion, data: ObjectValue, proto: {…}, hasLocalMutations: false, …}
_firestore: Firestore {_queue: AsyncQueue, INTERNAL: {…}, _config: FirestoreConfig, _databaseId: DatabaseId, _dataConverter: UserDataConverter, …}
_fromCache: false
_hasPendingWrites: false
_key: DocumentKey {path: ResourcePath}
__proto__: DocumentSnapshot
res.json(offset)
feedData = await dataWithOffset.json();
{ _ref:
{ _firestore:
{ _settings: [Object],
_settingsFrozen: true,
_serializer: [Object],
_projectId: '***-prod',
_lastSuccessfulRequest: 1566308918946,
_preferTransactions: false,
_clientPool: [Object] },
_path:
{ segments: [Array],
projectId: '***-prod',
databaseId: '(default)' } },
_fieldsProto:
{ lastModified: { timestampValue: [Object], valueType: 'timestampValue' },
...,
_serializer: { timestampsInSnapshots: true },
_readTime: { _seconds: 1566308918, _nanoseconds: 909566000 },
_createTime: { _seconds: 1565994031, _nanoseconds: 304997000 },
_updateTime: { _seconds: 1565994031, _nanoseconds: 304997000 } }
[
{
"name": "car 1",
"hp": 5
},
{
"name": "car 2",
"hp": 10
},
{
"name": "car 2.5",
"hp": 10
},
{
"name": "car 3",
"hp": 15
}
]
然后我配置了一个express端点:
router.get('/cars/:lastDocId', (req, res) => {
const query = db.collection('cars')
.orderBy('hp')
.limit(2);
const handleQueryRes = (snap) => {
return res.send({
docs: snap.docs.map(doc => doc.data()),
last: snap.docs.length > 0 ? snap.docs[snap.docs.length - 1].id : null
});
}
if(req.params.lastDocId != -1) {
// this makes an "auxiliar" read from the DB to transform the given ID in a DocumentSnapshot needed for startAfter
return db.collection('cars').doc(req.params.lastDocId).get().then(snap => {
return query.startAfter(snap).get();
}).then(handleQueryRes).catch(console.log);
} else {
return query.get().then(handleQueryRes).catch(console.log);
}
});
路径参数lastDocId对于第一页或上一页中获取的最后一个文档为-1
当我向/cars/-1发出GET请求时,它返回以下内容:
{
"docs": [
{
"hp": 5,
"name": "car 1"
},
{
"name": "car 2",
"hp": 10
}
],
"last": "9sRdLOvV8REwEpHDjEw7"
}
现在,如果我抓住响应中的最后一个道具并在下一个GET like so/cars/9sRdLOvV8REwEpHDjEw7中使用它,我将得到下两辆车:
{
"docs": [
{
"name": "car 2.5",
"hp": 10
},
{
"name": "car 3",
"hp": 15
}
],
"last": "tZPF7Wav7jZuchoCp6zM"
}
尽管我只有4条记录,第二个请求应该是最后一个,但函数不知道集合的长度,因此它返回另一个最后的ID
如果您发出最后一个请求/cars/tZPF7Wav7jZuchoCp6zM,它将返回:
{
"docs": [],
"last": null
}
因此给出了最后一页的指示
我不太喜欢再次阅读文档以将其转换为DocumentSnapshot,但我想这是firebase的局限性
希望有帮助
有步骤地回答
当转换为JSON并返回时,您将丢失DocumentReference原型
通过网络请求传递该类型数据的解决方案是:
客户端向您的端点发送纯文本引用,或者不发送第一页的引用
有服务器端代码吗
使用重新创建的DocumentReference对查询进行分页
从服务器返回请求页面的文档以及纯文本引用以获取下一页
初始答案已过时,但供将来参考
您需要发送offset.data,而不是同时发送offset
一个是文档的数据,可能是您所关注的,另一个是DocumentReference,它的方法不能用JSON表示
如果您需要文档中的其他内容,如id或引用,最好构建一个新对象:
res.json({
data: offset.data(),
ref: offset.ref,
id: offset.id
});
根据文档,并与以前的工作解决方案相比,快照是此处所期望的。一旦转换成数据、ref和ID,它就不再是快照了。如果不是原始格式,startAfter真正期望的是什么?这里的文档:如果您正在转换为JSON,您将丢失DocumentReference原型,如果您正在发出需要DocumentReference的请求,请使用ref属性并重新创建DocumentReference,如:firestore.doc赞赏此更新,但我认为您不能像这样重新创建文档引用,您可能需要它的完整路径集合和所有。如果您可以共享一些服务器端代码,我可能可以帮助您进一步了解它是完整的收集路径,id不是.doc中的ref。不过,对于startAfter来说,参考资料似乎不起作用。也没有支持它的文档。看起来快照是它所期望的,所以这不起作用。无论如何,谢谢你。