クエリからドキュメント参照を取得しようとしています。私のコードはを返しますundefined
。のさまざまな部分を抽出することでパスを取得できますdocumentSnapshot.ref
が、これは簡単ではありません。
私が返したいのは.update
、コレクションを指定して使用する必要なく、後でドキュメントに使用できる参照ですdocumentSnapshot.id
path
プロパティのドキュメントはこちらです
const db = admin.firestore();
return db.collection('myCollection').get().then(querySnapshot => {
querySnapshot.forEach(documentSnapshot => {
console.log(`documentReference.id = ${documentSnapshot.id}`);
console.log(`documentReference.path = ${documentSnapshot.path}`);
// console.log(`documentReference.ref = ${JSON.stringify(documentSnapshot.ref)}`);
});
});
documentReference.id = Jez7R1GAHiR9nbjS3CQ6
documentReference.path = undefined
documentReference.id = skMmxxUIFXPyVa7Ic7Yp
documentReference.path = undefined
コードでdocumentSnapshot
は、はDocumentSnapshotタイプのオブジェクトです。DocumentReference型のオブジェクトであると想定しているようです。参照の目的は、ドキュメントを見つけることです。スナップショットの目的は、クエリが実行された後にドキュメントのコンテンツを受け取ることです。これらはまったく同じものではありません。DocumentSnapshotにはpath
プロパティがありません。
DocumentSnapshotでフェッチされたドキュメントのDocumentReferenceが必要な場合は、スナップショットでrefを使用できます。次に、refのパスプロパティを取得できます。
documentSnapshot.ref.path
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加