Why doesn't startAfter work as expected on a recursive get() in firebase? The Next CEO of Stack OverflowWhy don't self-closing script tags work?Why doesn't indexOf work on an array IE8?Why does JavaScript only work after opening developer tools in IE once?Firebase limitToFirst doesn't work as expectedJavascript promise recursion and chainingRecursive wildcards in Firestore security rules not working as expectedFirebase StartAfter in query not working as expectedAngularfire2, startAfter() not working for paginationHow to avoid firebase functions recursionReturning cloud Firebase data from multiple promises at the same time

Do I need to enable Dev Hub in my PROD Org?

How should I support this large drywall patch?

WOW air has ceased operation, can I get my tickets refunded?

Help understanding this unsettling image of Titan, Epimetheus, and Saturn's rings?

How to make a variable always equal to the result of some calculations?

Workaholic Formal/Informal

What do "high sea" and "carry" mean in this sentence?

What is ( CFMCC ) on ILS approach chart?

Make solar eclipses exceedingly rare, but still have new moons

Unreliable Magic - Is it worth it?

If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?

Won the lottery - how do I keep the money?

What happened in Rome, when the western empire "fell"?

What was the first Unix version to run on a microcomputer?

I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin

Sending manuscript to multiple publishers

Is it ever safe to open a suspicious html file (e.g. email attachment)?

In excess I'm lethal

Anatomically Correct Strange Women In Ponds Distributing Swords

Does it take more energy to get to Venus or to Mars?

Received an invoice from my ex-employer billing me for training; how to handle?

What does convergence in distribution "in the Gromov–Hausdorff" sense mean?

Inappropriate reference requests from Journal reviewers

What is the purpose of the Evocation wizard's Potent Cantrip feature?



Why doesn't startAfter work as expected on a recursive get() in firebase?



The Next CEO of Stack OverflowWhy don't self-closing script tags work?Why doesn't indexOf work on an array IE8?Why does JavaScript only work after opening developer tools in IE once?Firebase limitToFirst doesn't work as expectedJavascript promise recursion and chainingRecursive wildcards in Firestore security rules not working as expectedFirebase StartAfter in query not working as expectedAngularfire2, startAfter() not working for paginationHow to avoid firebase functions recursionReturning cloud Firebase data from multiple promises at the same time










0















In a nutshell: when given the last object for a particular ordering, startAfter() ought to return empty results (I think), but it doesn't. Why not?)



I have a large collection that needs periodic maintenance. I'd like to do it by reading batches, doing batch writes, then carrying on recursively.

I think I've built the correct structure, as follows (this will be cloud code eventually. I'm running it in the browser to avoid the painful build-deploy-test loop using the cloud)



function cleanupFoos(startAfter) 
let query = db.collection('myCollection').orderBy('createdAt').limit(200)
if (startAfter) query.startAfter(startAfter)
return query.get().then(querySnapshot =>
startAfter = _.last(querySnapshot.docs)
let batch = db.batch()
let promises = querySnapshot.docs.map(doc => cleanupFoo(doc, batch))
return Promise.all(promises).then(() => batch)
).then(batch =>
return batch.commit()
).then(() =>
return startAfter ? cleanupFoos(startAfter) : null
)


function cleanupFoo(doc, batch)
// return a promise


// call it
cleanupFoos()


But this code loops infinitely. Watching in the debugger on a single document collection (myCollection: [ just this one doc ]), I get that single doc on the first invocation. It's also the last doc, so,



startAfter = _.last(querySnapshot.docs) // --> just this one doc 


On the recursive call, I can see that startAfter is set to what I expect: the first (and last) doc. My expectation is that get() should now return an empty querySnapshot. But it returns the same single doc, triggering another round, then another.... That's wrong, right?










share|improve this question




























    0















    In a nutshell: when given the last object for a particular ordering, startAfter() ought to return empty results (I think), but it doesn't. Why not?)



    I have a large collection that needs periodic maintenance. I'd like to do it by reading batches, doing batch writes, then carrying on recursively.

    I think I've built the correct structure, as follows (this will be cloud code eventually. I'm running it in the browser to avoid the painful build-deploy-test loop using the cloud)



    function cleanupFoos(startAfter) 
    let query = db.collection('myCollection').orderBy('createdAt').limit(200)
    if (startAfter) query.startAfter(startAfter)
    return query.get().then(querySnapshot =>
    startAfter = _.last(querySnapshot.docs)
    let batch = db.batch()
    let promises = querySnapshot.docs.map(doc => cleanupFoo(doc, batch))
    return Promise.all(promises).then(() => batch)
    ).then(batch =>
    return batch.commit()
    ).then(() =>
    return startAfter ? cleanupFoos(startAfter) : null
    )


    function cleanupFoo(doc, batch)
    // return a promise


    // call it
    cleanupFoos()


    But this code loops infinitely. Watching in the debugger on a single document collection (myCollection: [ just this one doc ]), I get that single doc on the first invocation. It's also the last doc, so,



    startAfter = _.last(querySnapshot.docs) // --> just this one doc 


    On the recursive call, I can see that startAfter is set to what I expect: the first (and last) doc. My expectation is that get() should now return an empty querySnapshot. But it returns the same single doc, triggering another round, then another.... That's wrong, right?










    share|improve this question


























      0












      0








      0








      In a nutshell: when given the last object for a particular ordering, startAfter() ought to return empty results (I think), but it doesn't. Why not?)



      I have a large collection that needs periodic maintenance. I'd like to do it by reading batches, doing batch writes, then carrying on recursively.

      I think I've built the correct structure, as follows (this will be cloud code eventually. I'm running it in the browser to avoid the painful build-deploy-test loop using the cloud)



      function cleanupFoos(startAfter) 
      let query = db.collection('myCollection').orderBy('createdAt').limit(200)
      if (startAfter) query.startAfter(startAfter)
      return query.get().then(querySnapshot =>
      startAfter = _.last(querySnapshot.docs)
      let batch = db.batch()
      let promises = querySnapshot.docs.map(doc => cleanupFoo(doc, batch))
      return Promise.all(promises).then(() => batch)
      ).then(batch =>
      return batch.commit()
      ).then(() =>
      return startAfter ? cleanupFoos(startAfter) : null
      )


      function cleanupFoo(doc, batch)
      // return a promise


      // call it
      cleanupFoos()


      But this code loops infinitely. Watching in the debugger on a single document collection (myCollection: [ just this one doc ]), I get that single doc on the first invocation. It's also the last doc, so,



      startAfter = _.last(querySnapshot.docs) // --> just this one doc 


      On the recursive call, I can see that startAfter is set to what I expect: the first (and last) doc. My expectation is that get() should now return an empty querySnapshot. But it returns the same single doc, triggering another round, then another.... That's wrong, right?










      share|improve this question
















      In a nutshell: when given the last object for a particular ordering, startAfter() ought to return empty results (I think), but it doesn't. Why not?)



      I have a large collection that needs periodic maintenance. I'd like to do it by reading batches, doing batch writes, then carrying on recursively.

      I think I've built the correct structure, as follows (this will be cloud code eventually. I'm running it in the browser to avoid the painful build-deploy-test loop using the cloud)



      function cleanupFoos(startAfter) 
      let query = db.collection('myCollection').orderBy('createdAt').limit(200)
      if (startAfter) query.startAfter(startAfter)
      return query.get().then(querySnapshot =>
      startAfter = _.last(querySnapshot.docs)
      let batch = db.batch()
      let promises = querySnapshot.docs.map(doc => cleanupFoo(doc, batch))
      return Promise.all(promises).then(() => batch)
      ).then(batch =>
      return batch.commit()
      ).then(() =>
      return startAfter ? cleanupFoos(startAfter) : null
      )


      function cleanupFoo(doc, batch)
      // return a promise


      // call it
      cleanupFoos()


      But this code loops infinitely. Watching in the debugger on a single document collection (myCollection: [ just this one doc ]), I get that single doc on the first invocation. It's also the last doc, so,



      startAfter = _.last(querySnapshot.docs) // --> just this one doc 


      On the recursive call, I can see that startAfter is set to what I expect: the first (and last) doc. My expectation is that get() should now return an empty querySnapshot. But it returns the same single doc, triggering another round, then another.... That's wrong, right?







      javascript firebase google-cloud-firestore






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 21 at 18:25







      goodson

















      asked Mar 21 at 16:46









      goodsongoodson

      195212




      195212






















          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%2f55285405%2fwhy-doesnt-startafter-work-as-expected-on-a-recursive-get-in-firebase%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.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55285405%2fwhy-doesnt-startafter-work-as-expected-on-a-recursive-get-in-firebase%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







          Popular posts from this blog

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

          155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해