英文:
Firestore copy whole map in field to another document
问题
我有一个名为ALL_quote
的集合,其中包含我已经生成的哈希,里面有一个名为tmp_ff
的地图,其中包含我想要复制到另一个集合的必要信息。
如您在图片中所见,我已经在ALL-quote
中有tmp_ff
作为一个地图,我想要复制tmp_ff
中的所有元素到ALL_invoice
。以下是我迄今为止所做的:
const copyQuoteProduct = firebase.firestore().collection('ALL_quote').where("quote_hashid", "==", "9hgr9Myhc70CUalKnT1u");
await copyQuoteProduct.get().then(function(querySnapshot) {
querySnapshot.docs.map(function(doc) {
console.log("===", doc.tmp_ff); //undefined
this.copy_tmp_ff.push(doc.tmp_ff.q_p1_category); //也是undefined,只是测试
})
});
我认为快照是正确的,为什么我从doc.tmp_ff
中得不到任何东西呢?如果我想要复制精确的元素到ALL_invoice
,并且我应该有一个名为copy_tmp_ff
的空地图,如何将doc.tmp_ff
中的所有内容声明到copy_tmp_ff
中?
然后,我将像这样将所有的copy_tmp_ff
推送到ALL_quote
:
const ref = collection(db, "ALL_invoice");
addDoc(ref, copy_tmp_ff) //现在是空的
英文:
I have a collection ALL_quote
with a hash I generate already inside there is a map called tmp_ff
with the necessary I want to copy to another collection.
As you see from the picture, I have tmp_ff
as a map already inside ALL-quote
, I want to copy all elements inside tmp_ff
to ALL_invoice
. Here is what I have so far:
const copyQuoteProduct = firebase.firestore().collection('ALL_quote').where("quote_hashid", "==", "9hgr9Myhc70CUalKnT1u");
await copyQuoteProduct.get().then(function(querySnapshot) {
querySnapshot.docs.map(function(doc) {
console.log("===", doc.tmp_ff); //undefined
this.copy_tmp_ff.push(doc.tmp_ff.q_p1_category); //undefined also, just test
})
});
I believe snapshot is correct, how come I get nothing from doc.tmp_ff
? If I want to copy exact element to ALL_invoice
, and I suppose to have empty map called copy_tmp_ff
how can I declare everything from doc.tmp_ff
to copy_tmp_ff
?
And I will push all copy_tmp_ff
like this to ALL_quote
:
const ref = collection(db, "ALL_invoice");
addDoc(ref, copy_tmp_ff) //empty right now
答案1
得分: 0
以下是您要翻译的内容:
如果要将新文档添加到ALL_invoice
,并使用您标记的数据,可以这样做:
const copyQuoteProduct = firebase.firestore().collection('ALL_quote').where("quote_hashid", "==", "9hgr9Myhc70CUalKnT1u");
const allInvoiceRef = firebase.firestore().collection('ALL_invoice');
await copyQuoteProduct.get().then(function(querySnapshot) {
querySnapshot.docs.map(function(doc) {
allInvoiceRef.add({ tmp_ff: doc.data().tmp_ff });
})
});
如果quote_hashid
始终与文档ID相同,您无需查询或循环,可以简化如下:
const copyQuoteProduct = firebase.firestore().collection('ALL_quote').doc("9hgr9Myhc70CUalKnT1u");
const allInvoiceRef = firebase.firestore().collection('ALL_invoice');
await copyQuoteProduct.get().then(function(doc) {
allInvoiceRef.add(allInvoiceRef, { tmp_ff: doc.data().tmp_ff });
});
英文:
If you want to add a new document to ALL_invoice
with the data that you marked, that'd be:
const copyQuoteProduct = firebase.firestore().collection('ALL_quote').where("quote_hashid", "==", "9hgr9Myhc70CUalKnT1u");
const allInvoiceRef = firebase.firestore().collection('ALL_invoice');
await copyQuoteProduct.get().then(function(querySnapshot) {
querySnapshot.docs.map(function(doc) {
allInvoiceRef.add({ tmp_ff: doc.data().tmp_ff });
})
});
If quote_hashid
is always the same value as the document ID, you don't need the query or loop, and can simplify the above to:
const copyQuoteProduct = firebase.firestore().collection('ALL_quote').doc("9hgr9Myhc70CUalKnT1u");
const allInvoiceRef = firebase.firestore().collection('ALL_invoice');
await copyQuoteProduct.get().then(function(doc) {
allInvoiceRef.add(allInvoiceRef, { tmp_ff: doc.data().tmp_ff });
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论