Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 有没有一种方法可以将文档值复制到另一个文档中,而无需提取数据并发回_Javascript_Firebase_Google Cloud Firestore - Fatal编程技术网

Javascript 有没有一种方法可以将文档值复制到另一个文档中,而无需提取数据并发回

Javascript 有没有一种方法可以将文档值复制到另一个文档中,而无需提取数据并发回,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,我有2个文档帖子和博文。在某种程度上,我想将文档从postDraft发布到post。 内容类似于{body:blah blah} 因此,简单的解决方案是提取postDraft内容,然后发送回posts文档。这是我的代码 export const publishDraftById = (postId) => async dispatch => { return new Promise((resolve, reject) => { postDraf

我有2个文档帖子和博文。在某种程度上,我想将文档从postDraft发布到post。 内容类似于{body:blah blah}

因此,简单的解决方案是提取postDraft内容,然后发送回posts文档。这是我的代码

   export const publishDraftById = (postId) => async dispatch => {
      return new Promise((resolve, reject) => {
        postDraftsRef.doc(postId).get()
          .then(doc => {
            if (!doc.exists) {
              reject('No such document!');
            } else {
              const draft = doc.data();
              postsRef.doc(postId).update({
                body: draft.body,
                date_modified: new Date()
              });
              resolve();
            }
          })
          .catch((err) => {
            reject(err);
            console.log('Error getting documents', err);
          });
      })
    };
但是我真正认为有帮助的是一种方法来做一些类似于元代码的事情

分配postspostid.body=postDraftpostid.body

我能设法做到这一点吗

有没有一种方法可以将文档值复制到另一个文档中,而无需提取数据并发回

不,没有。您需要先获取文档,才能将其写回。如果不这样做,就无法简单地获取文档中的属性并将其写入另一个文档中。没有对文档的字段级权限或访问权限。这是整个文件,或者什么都没有


因此,如果你想读取一个字段{body:blah blah blah,那么你将获得整个文档,而不仅仅是你已经将其写入另一个文档的属性。因此涉及两个操作,一个读取操作和一个写入操作。

嗨,Tigran!一切都好吗,我可以为你提供其他信息吗?