BSo*_*SoD 6 node.js google-cloud-functions google-cloud-firestore
我只是想在我的一个 firebase 可调用函数中获取文档数据,我已经从之前的查询中知道了它的 ID。
问题是[...].doc(id).get()方法的结果对象没有.data()方法或exists成员。
我尝试使用列出对象的属性Object.getOwnPropertyNames(snapshot);,但找不到exists或data()。
使用 获取文档的参考作品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
};
}
})
Run Code Online (Sandbox Code Playgroud)
这是我的第一个 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 } } ]
Run Code Online (Sandbox Code Playgroud)
这是我的第二个 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 } }
Run Code Online (Sandbox Code Playgroud)
奇怪的是,我可以在第一个快照上使用 .data() 就好了,但在第二个快照上不存在,即使两个日志都表明两个快照都是 QueryDocumentSnapshot 类型。
| 归档时间: |
|
| 查看次数: |
984 次 |
| 最近记录: |