Convert query string to date object









up vote
1
down vote

favorite












I have a GET endpoint
/internal/users?role=057426a1235fa1084c6f&lastLoggedIn[$lte]=2018-11-07T22:57:44.612Z



I am using featherjs and feathers-rest client to call my services.



My problem is I get the req.query as

role: '057426a1235fa1084c6f',
lastLogged: $lte: '2018-11-07T22:57:44.612Z'

and I am getting date as string not date object.



is there any way I can transform my query to have the correct date object if I encountered any ISODate string?



I have tried something like






app.use((req, res, next) => 
const queryFlat = flat(req.query);
for (key in queryFlat)
queryFlat[key] = dateCheck(queryFlat[key]) // dateCheck returns date object if ISODate String

req.query = unlfat(queryFlat)
);





This is working fine, but is there any way I can pass to qs.parser which can automatically convert date string to date Object?










share|improve this question



























    up vote
    1
    down vote

    favorite












    I have a GET endpoint
    /internal/users?role=057426a1235fa1084c6f&lastLoggedIn[$lte]=2018-11-07T22:57:44.612Z



    I am using featherjs and feathers-rest client to call my services.



    My problem is I get the req.query as

    role: '057426a1235fa1084c6f',
    lastLogged: $lte: '2018-11-07T22:57:44.612Z'

    and I am getting date as string not date object.



    is there any way I can transform my query to have the correct date object if I encountered any ISODate string?



    I have tried something like






    app.use((req, res, next) => 
    const queryFlat = flat(req.query);
    for (key in queryFlat)
    queryFlat[key] = dateCheck(queryFlat[key]) // dateCheck returns date object if ISODate String

    req.query = unlfat(queryFlat)
    );





    This is working fine, but is there any way I can pass to qs.parser which can automatically convert date string to date Object?










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a GET endpoint
      /internal/users?role=057426a1235fa1084c6f&lastLoggedIn[$lte]=2018-11-07T22:57:44.612Z



      I am using featherjs and feathers-rest client to call my services.



      My problem is I get the req.query as

      role: '057426a1235fa1084c6f',
      lastLogged: $lte: '2018-11-07T22:57:44.612Z'

      and I am getting date as string not date object.



      is there any way I can transform my query to have the correct date object if I encountered any ISODate string?



      I have tried something like






      app.use((req, res, next) => 
      const queryFlat = flat(req.query);
      for (key in queryFlat)
      queryFlat[key] = dateCheck(queryFlat[key]) // dateCheck returns date object if ISODate String

      req.query = unlfat(queryFlat)
      );





      This is working fine, but is there any way I can pass to qs.parser which can automatically convert date string to date Object?










      share|improve this question















      I have a GET endpoint
      /internal/users?role=057426a1235fa1084c6f&lastLoggedIn[$lte]=2018-11-07T22:57:44.612Z



      I am using featherjs and feathers-rest client to call my services.



      My problem is I get the req.query as

      role: '057426a1235fa1084c6f',
      lastLogged: $lte: '2018-11-07T22:57:44.612Z'

      and I am getting date as string not date object.



      is there any way I can transform my query to have the correct date object if I encountered any ISODate string?



      I have tried something like






      app.use((req, res, next) => 
      const queryFlat = flat(req.query);
      for (key in queryFlat)
      queryFlat[key] = dateCheck(queryFlat[key]) // dateCheck returns date object if ISODate String

      req.query = unlfat(queryFlat)
      );





      This is working fine, but is there any way I can pass to qs.parser which can automatically convert date string to date Object?






      app.use((req, res, next) => 
      const queryFlat = flat(req.query);
      for (key in queryFlat)
      queryFlat[key] = dateCheck(queryFlat[key]) // dateCheck returns date object if ISODate String

      req.query = unlfat(queryFlat)
      );





      app.use((req, res, next) => 
      const queryFlat = flat(req.query);
      for (key in queryFlat)
      queryFlat[key] = dateCheck(queryFlat[key]) // dateCheck returns date object if ISODate String

      req.query = unlfat(queryFlat)
      );






      express query-string feathersjs






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 7 at 23:21









      Shankar Regmi

      522213




      522213










      asked Nov 7 at 23:18









      Harish Paudel

      16629




      16629






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          I think your flat and unflat can have problem if you pass some query with keys containing . itself.



          so, this can help,
          using qs library



          app.use(req, res, next) => 
          req.query = qs.parse(qs.stringify(req.query),
          decoder: dateCheck, // dateCheck returns date object if ISODate String
          );



          Your dateCheck function should be something like this



          const dateCheck = (value, decoder) => 
          const key = decoder(value); // this
          // ... rest of date check function






          share|improve this answer






















          • perfect, it works as expected
            – Harish Paudel
            Nov 10 at 14:06










          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',
          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%2f53199364%2fconvert-query-string-to-date-object%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          I think your flat and unflat can have problem if you pass some query with keys containing . itself.



          so, this can help,
          using qs library



          app.use(req, res, next) => 
          req.query = qs.parse(qs.stringify(req.query),
          decoder: dateCheck, // dateCheck returns date object if ISODate String
          );



          Your dateCheck function should be something like this



          const dateCheck = (value, decoder) => 
          const key = decoder(value); // this
          // ... rest of date check function






          share|improve this answer






















          • perfect, it works as expected
            – Harish Paudel
            Nov 10 at 14:06














          up vote
          1
          down vote



          accepted










          I think your flat and unflat can have problem if you pass some query with keys containing . itself.



          so, this can help,
          using qs library



          app.use(req, res, next) => 
          req.query = qs.parse(qs.stringify(req.query),
          decoder: dateCheck, // dateCheck returns date object if ISODate String
          );



          Your dateCheck function should be something like this



          const dateCheck = (value, decoder) => 
          const key = decoder(value); // this
          // ... rest of date check function






          share|improve this answer






















          • perfect, it works as expected
            – Harish Paudel
            Nov 10 at 14:06












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          I think your flat and unflat can have problem if you pass some query with keys containing . itself.



          so, this can help,
          using qs library



          app.use(req, res, next) => 
          req.query = qs.parse(qs.stringify(req.query),
          decoder: dateCheck, // dateCheck returns date object if ISODate String
          );



          Your dateCheck function should be something like this



          const dateCheck = (value, decoder) => 
          const key = decoder(value); // this
          // ... rest of date check function






          share|improve this answer














          I think your flat and unflat can have problem if you pass some query with keys containing . itself.



          so, this can help,
          using qs library



          app.use(req, res, next) => 
          req.query = qs.parse(qs.stringify(req.query),
          decoder: dateCheck, // dateCheck returns date object if ISODate String
          );



          Your dateCheck function should be something like this



          const dateCheck = (value, decoder) => 
          const key = decoder(value); // this
          // ... rest of date check function







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 14:06

























          answered Nov 10 at 14:00









          Shankar Regmi

          522213




          522213











          • perfect, it works as expected
            – Harish Paudel
            Nov 10 at 14:06
















          • perfect, it works as expected
            – Harish Paudel
            Nov 10 at 14:06















          perfect, it works as expected
          – Harish Paudel
          Nov 10 at 14:06




          perfect, it works as expected
          – Harish Paudel
          Nov 10 at 14:06

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53199364%2fconvert-query-string-to-date-object%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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3

          Museum of Modern and Contemporary Art of Trento and Rovereto