Change values of one firestore collection with data of a second one












0














I am a bit confused, I am trying to build 1 response by querying one collection, and trying to change some values of the first observable with values of a related query.



i.e.

1. On the first collection I have the system categories with 'position' that is the default value:

Categories = [{name:'cat1', id:'id', position:1},...]

2. on the second one I have the user values /user_id/categories

UserCategories = [{cat_id:1, position:3},...]



Now I need to change the position value, based on the user, so I have this on my service:



  getCategoriesAfsOld() {
this.categoriesCollection = this.afs.collection('category');
return this.categoriesCollection
.snapshotChanges()
.map(category => {
return category.map(data => {
const catData = data.payload.doc.data() as Category;
const usrData = this.afs.doc(`/user/${this.auth.getUserID()}/category/${data.payload.doc.id}`)
.valueChanges();
usrData.subscribe( (catByUsr: CatByUser) =>{
if(typeof catByUsr !== "undefined"){
catData.position = catByUsr.position;
}
}
);
catData.id = data.payload.doc.id;
return catData;
});
});
}


Now, this almost does the trick, but because I am using map(), it returns the the data unchanged and after a short time it sends the data changed again with the new positions.

As you might guess this is not good, I need to send the data modified on the first call,and probably stop the stream. I know this can be accomplished by using switchMap, I have not being able to do this.



I am doing something really wrong or maybe looking in the wrong direction.



Please some help or guidance.



The following is one of many failed tries:



  getCategoriesAfs() {
this.categoriesCollection = this.afs.collection('category');
return this.categoriesCollection
.snapshotChanges()
.switchMap(category => {
return category.map(data => {
const catData = data.payload.doc.data() as Category;
catData.id = data.payload.doc.id;
const usrData = this.afs
.doc(`/user/${this.auth.getUserID()}/category/${data.payload.doc.id}`)
.valueChanges().switchMap(((value) => {
const data = ;
return data;
}));
usrData.subscribe( (catByUsr) =>{
if(typeof catByUsr !== "undefined"){
catData.position = catByUsr.position;
}
}
);
return catData;
});
});
}









share|improve this question





























    0














    I am a bit confused, I am trying to build 1 response by querying one collection, and trying to change some values of the first observable with values of a related query.



    i.e.

    1. On the first collection I have the system categories with 'position' that is the default value:

    Categories = [{name:'cat1', id:'id', position:1},...]

    2. on the second one I have the user values /user_id/categories

    UserCategories = [{cat_id:1, position:3},...]



    Now I need to change the position value, based on the user, so I have this on my service:



      getCategoriesAfsOld() {
    this.categoriesCollection = this.afs.collection('category');
    return this.categoriesCollection
    .snapshotChanges()
    .map(category => {
    return category.map(data => {
    const catData = data.payload.doc.data() as Category;
    const usrData = this.afs.doc(`/user/${this.auth.getUserID()}/category/${data.payload.doc.id}`)
    .valueChanges();
    usrData.subscribe( (catByUsr: CatByUser) =>{
    if(typeof catByUsr !== "undefined"){
    catData.position = catByUsr.position;
    }
    }
    );
    catData.id = data.payload.doc.id;
    return catData;
    });
    });
    }


    Now, this almost does the trick, but because I am using map(), it returns the the data unchanged and after a short time it sends the data changed again with the new positions.

    As you might guess this is not good, I need to send the data modified on the first call,and probably stop the stream. I know this can be accomplished by using switchMap, I have not being able to do this.



    I am doing something really wrong or maybe looking in the wrong direction.



    Please some help or guidance.



    The following is one of many failed tries:



      getCategoriesAfs() {
    this.categoriesCollection = this.afs.collection('category');
    return this.categoriesCollection
    .snapshotChanges()
    .switchMap(category => {
    return category.map(data => {
    const catData = data.payload.doc.data() as Category;
    catData.id = data.payload.doc.id;
    const usrData = this.afs
    .doc(`/user/${this.auth.getUserID()}/category/${data.payload.doc.id}`)
    .valueChanges().switchMap(((value) => {
    const data = ;
    return data;
    }));
    usrData.subscribe( (catByUsr) =>{
    if(typeof catByUsr !== "undefined"){
    catData.position = catByUsr.position;
    }
    }
    );
    return catData;
    });
    });
    }









    share|improve this question



























      0












      0








      0







      I am a bit confused, I am trying to build 1 response by querying one collection, and trying to change some values of the first observable with values of a related query.



      i.e.

      1. On the first collection I have the system categories with 'position' that is the default value:

      Categories = [{name:'cat1', id:'id', position:1},...]

      2. on the second one I have the user values /user_id/categories

      UserCategories = [{cat_id:1, position:3},...]



      Now I need to change the position value, based on the user, so I have this on my service:



        getCategoriesAfsOld() {
      this.categoriesCollection = this.afs.collection('category');
      return this.categoriesCollection
      .snapshotChanges()
      .map(category => {
      return category.map(data => {
      const catData = data.payload.doc.data() as Category;
      const usrData = this.afs.doc(`/user/${this.auth.getUserID()}/category/${data.payload.doc.id}`)
      .valueChanges();
      usrData.subscribe( (catByUsr: CatByUser) =>{
      if(typeof catByUsr !== "undefined"){
      catData.position = catByUsr.position;
      }
      }
      );
      catData.id = data.payload.doc.id;
      return catData;
      });
      });
      }


      Now, this almost does the trick, but because I am using map(), it returns the the data unchanged and after a short time it sends the data changed again with the new positions.

      As you might guess this is not good, I need to send the data modified on the first call,and probably stop the stream. I know this can be accomplished by using switchMap, I have not being able to do this.



      I am doing something really wrong or maybe looking in the wrong direction.



      Please some help or guidance.



      The following is one of many failed tries:



        getCategoriesAfs() {
      this.categoriesCollection = this.afs.collection('category');
      return this.categoriesCollection
      .snapshotChanges()
      .switchMap(category => {
      return category.map(data => {
      const catData = data.payload.doc.data() as Category;
      catData.id = data.payload.doc.id;
      const usrData = this.afs
      .doc(`/user/${this.auth.getUserID()}/category/${data.payload.doc.id}`)
      .valueChanges().switchMap(((value) => {
      const data = ;
      return data;
      }));
      usrData.subscribe( (catByUsr) =>{
      if(typeof catByUsr !== "undefined"){
      catData.position = catByUsr.position;
      }
      }
      );
      return catData;
      });
      });
      }









      share|improve this question















      I am a bit confused, I am trying to build 1 response by querying one collection, and trying to change some values of the first observable with values of a related query.



      i.e.

      1. On the first collection I have the system categories with 'position' that is the default value:

      Categories = [{name:'cat1', id:'id', position:1},...]

      2. on the second one I have the user values /user_id/categories

      UserCategories = [{cat_id:1, position:3},...]



      Now I need to change the position value, based on the user, so I have this on my service:



        getCategoriesAfsOld() {
      this.categoriesCollection = this.afs.collection('category');
      return this.categoriesCollection
      .snapshotChanges()
      .map(category => {
      return category.map(data => {
      const catData = data.payload.doc.data() as Category;
      const usrData = this.afs.doc(`/user/${this.auth.getUserID()}/category/${data.payload.doc.id}`)
      .valueChanges();
      usrData.subscribe( (catByUsr: CatByUser) =>{
      if(typeof catByUsr !== "undefined"){
      catData.position = catByUsr.position;
      }
      }
      );
      catData.id = data.payload.doc.id;
      return catData;
      });
      });
      }


      Now, this almost does the trick, but because I am using map(), it returns the the data unchanged and after a short time it sends the data changed again with the new positions.

      As you might guess this is not good, I need to send the data modified on the first call,and probably stop the stream. I know this can be accomplished by using switchMap, I have not being able to do this.



      I am doing something really wrong or maybe looking in the wrong direction.



      Please some help or guidance.



      The following is one of many failed tries:



        getCategoriesAfs() {
      this.categoriesCollection = this.afs.collection('category');
      return this.categoriesCollection
      .snapshotChanges()
      .switchMap(category => {
      return category.map(data => {
      const catData = data.payload.doc.data() as Category;
      catData.id = data.payload.doc.id;
      const usrData = this.afs
      .doc(`/user/${this.auth.getUserID()}/category/${data.payload.doc.id}`)
      .valueChanges().switchMap(((value) => {
      const data = ;
      return data;
      }));
      usrData.subscribe( (catByUsr) =>{
      if(typeof catByUsr !== "undefined"){
      catData.position = catByUsr.position;
      }
      }
      );
      return catData;
      });
      });
      }






      javascript firebase google-cloud-firestore angularfire2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 19:29









      Frank van Puffelen

      227k28373396




      227k28373396










      asked Nov 12 '18 at 19:23









      moplin

      1,382922




      1,382922
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268783%2fchange-values-of-one-firestore-collection-with-data-of-a-second-one%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268783%2fchange-values-of-one-firestore-collection-with-data-of-a-second-one%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini