gpt4 book ai didi

node.js - 云函数中的 QueryDocumentSnapshot 中不存在 data() 方法

转载 作者:行者123 更新时间:2023-12-03 12:11:58 26 4
gpt4 key购买 nike

我只是想在我的 firebase 可调用函数之一中获取文档数据,我已经从之前的查询中知道了它的 ID。

问题是来自 [...].doc(id).get() 的结果对象方法没有 .data()方法或 exists成员。

我尝试使用 Object.getOwnPropertyNames(snapshot); 列出对象的属性并且找不到 existsdata() .

使用 ref 获取文档的引用作品,但我无权访问其他方法/成员。

以下是我使用两个查询获取数据的方法(注意我的 console.log 调用):

return firestore.collection("/teams").where("login", "==", data.login).get()
.then( (snapshot) => {
if(!snapshot.empty) {
user = snapshot.docs[0].data().login;
id = snapshot.docs[0].id;
console.log(snapshot.docs);
return firestore.collection("/passwords").doc(id).get();
} else {
return {
success: false,
error: 2
};
}
})
.then((snapshot) => {
console.log(snapshot);
if(snapshot.hasOwnProperty("exists") && snapshot.exists) {
return bcrypt.compare(data.password, snapshot.data().password);
} else {
return {
success: false,
error: 2
};
}
})

这是我的第一个 console.log 的输出(我已经切断了验证器部分以节省空间):
[ QueryDocumentSnapshot {
_ref:
DocumentReference {
_firestore: [Object],
_validator: [Object],
_referencePath: [Object] },
_fieldsProto:
{ colour: [Object],
name: [Object],
teammates: [Object],
login: [Object] },
_serializer:
Serializer {
timestampsInSnapshotsEnabled: true,
createReference: [Function] },
_validator:
Validator {
... },
_readTime: Timestamp { _seconds: 1553530847, _nanoseconds: 875308000 },
_createTime: Timestamp { _seconds: 1553530458, _nanoseconds: 66673000 },
_updateTime: Timestamp { _seconds: 1553530458, _nanoseconds: 66673000 } } ]

这是我的第二个 console.log 的输出:
QueryDocumentSnapshot {
_ref:
DocumentReference {
_firestore:
Firestore {
_validator: [Object],
_clientPool: [Object],
_settingsFrozen: true,
_clientInitialized: [Object],
_initalizationSettings: [Object],
_serializer: [Object],
_timestampsInSnapshotsEnabled: true,
_referencePath: [Object],
_preferTransactions: true,
_lastSuccessfulRequest: 1553530848171 },
_validator:
Validator {
... },
_referencePath:
ResourcePath {
segments: [Array],
projectId: 'rally-gccd',
databaseId: '(default)' } },
_fieldsProto:
{ password:
{ stringValue: '$2b$10$Xy1fCI.mKxvBhCHGimNqK.mPGU4lb1p.6pQgqacnnAUYjPomIWyaa',
valueType: 'stringValue' } },
_serializer:
Serializer {
timestampsInSnapshotsEnabled: true,
createReference: [Function] },
_validator:
Validator {
... },
_readTime: Timestamp { _seconds: 1553530848, _nanoseconds: 114930000 },
_createTime: Timestamp { _seconds: 1553530458, _nanoseconds: 341760000 },
_updateTime: Timestamp { _seconds: 1553530458, _nanoseconds: 341760000 } }

奇怪的是,我可以在第一个快照上使用 .data() 就好了,但在第二个快照上不存在,即使两个日志都表明两个快照都是 QueryDocumentSnapshot 类型。

最佳答案

我通过在第一个文档查询 Promise 中嵌套第二个文档 get Promise 来解决这个问题。这不太理想,因为它有点凌乱,但至少它有效。

关于node.js - 云函数中的 QueryDocumentSnapshot 中不存在 data() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55334674/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com