Firestore将整个字段中的映射复制到另一个文档中

huangapple go评论65阅读模式
英文:

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.

Firestore将整个字段中的映射复制到另一个文档中

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 });
});

huangapple
  • 本文由 发表于 2023年3月3日 22:14:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628182.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定