Assigning a Date Object to update












1














I would like to know how to assign a date object in this scenario, I need to update lastUpdate, whenever user changes his details.



I also tried Object.assign(user.lastUpdated, new Date());



exports.edit = (req, res, next) => {
const userid = req.params.id;
const errorHandler = (error) => {
next(error);
};
const updateUser = (user) => {
Object.assign(user, req.body);
Object.assign(user.lastUpdated, new Date());// not working
user.lastUpdated= new Date(); //not able to save this in database
user.save().then(() => {
res.json({
uid: user.id,
username: user.username,
displayName: user.displayName,
password:user.password,
lastUpdated: user.lastUpdated// result should be last updated Date.

});
}).catch(errorHandler);
};
};









share|improve this question



























    1














    I would like to know how to assign a date object in this scenario, I need to update lastUpdate, whenever user changes his details.



    I also tried Object.assign(user.lastUpdated, new Date());



    exports.edit = (req, res, next) => {
    const userid = req.params.id;
    const errorHandler = (error) => {
    next(error);
    };
    const updateUser = (user) => {
    Object.assign(user, req.body);
    Object.assign(user.lastUpdated, new Date());// not working
    user.lastUpdated= new Date(); //not able to save this in database
    user.save().then(() => {
    res.json({
    uid: user.id,
    username: user.username,
    displayName: user.displayName,
    password:user.password,
    lastUpdated: user.lastUpdated// result should be last updated Date.

    });
    }).catch(errorHandler);
    };
    };









    share|improve this question

























      1












      1








      1







      I would like to know how to assign a date object in this scenario, I need to update lastUpdate, whenever user changes his details.



      I also tried Object.assign(user.lastUpdated, new Date());



      exports.edit = (req, res, next) => {
      const userid = req.params.id;
      const errorHandler = (error) => {
      next(error);
      };
      const updateUser = (user) => {
      Object.assign(user, req.body);
      Object.assign(user.lastUpdated, new Date());// not working
      user.lastUpdated= new Date(); //not able to save this in database
      user.save().then(() => {
      res.json({
      uid: user.id,
      username: user.username,
      displayName: user.displayName,
      password:user.password,
      lastUpdated: user.lastUpdated// result should be last updated Date.

      });
      }).catch(errorHandler);
      };
      };









      share|improve this question













      I would like to know how to assign a date object in this scenario, I need to update lastUpdate, whenever user changes his details.



      I also tried Object.assign(user.lastUpdated, new Date());



      exports.edit = (req, res, next) => {
      const userid = req.params.id;
      const errorHandler = (error) => {
      next(error);
      };
      const updateUser = (user) => {
      Object.assign(user, req.body);
      Object.assign(user.lastUpdated, new Date());// not working
      user.lastUpdated= new Date(); //not able to save this in database
      user.save().then(() => {
      res.json({
      uid: user.id,
      username: user.username,
      displayName: user.displayName,
      password:user.password,
      lastUpdated: user.lastUpdated// result should be last updated Date.

      });
      }).catch(errorHandler);
      };
      };






      javascript datetime javascript-objects






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 8:06









      darshan a ndarshan a n

      326114




      326114
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object. ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign ).



          But in your code Object.assign(user.lastUpdated, new Date()); what you are trying to do is join two values together. So it won't work.



          Try like this : Object.assign( user, { lastUpdated : new Date() } );






          share|improve this answer

















          • 2




            It would "work" if user.lastUpdated is an object (we don't know whether it is or not). It copies the enumerable property names and values, but since a Date instance has no enumerable own properties, it does nothing. ;-) I think it would be better to use user.lastUpdated = new Date().toISOString() or whatever format the database expects.
            – RobG
            Nov 21 '18 at 8:27












          • you are right @RobG
            – darshan a n
            Nov 21 '18 at 8:29











          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%2f53407626%2fassigning-a-date-object-to-update%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









          1














          The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object. ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign ).



          But in your code Object.assign(user.lastUpdated, new Date()); what you are trying to do is join two values together. So it won't work.



          Try like this : Object.assign( user, { lastUpdated : new Date() } );






          share|improve this answer

















          • 2




            It would "work" if user.lastUpdated is an object (we don't know whether it is or not). It copies the enumerable property names and values, but since a Date instance has no enumerable own properties, it does nothing. ;-) I think it would be better to use user.lastUpdated = new Date().toISOString() or whatever format the database expects.
            – RobG
            Nov 21 '18 at 8:27












          • you are right @RobG
            – darshan a n
            Nov 21 '18 at 8:29
















          1














          The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object. ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign ).



          But in your code Object.assign(user.lastUpdated, new Date()); what you are trying to do is join two values together. So it won't work.



          Try like this : Object.assign( user, { lastUpdated : new Date() } );






          share|improve this answer

















          • 2




            It would "work" if user.lastUpdated is an object (we don't know whether it is or not). It copies the enumerable property names and values, but since a Date instance has no enumerable own properties, it does nothing. ;-) I think it would be better to use user.lastUpdated = new Date().toISOString() or whatever format the database expects.
            – RobG
            Nov 21 '18 at 8:27












          • you are right @RobG
            – darshan a n
            Nov 21 '18 at 8:29














          1












          1








          1






          The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object. ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign ).



          But in your code Object.assign(user.lastUpdated, new Date()); what you are trying to do is join two values together. So it won't work.



          Try like this : Object.assign( user, { lastUpdated : new Date() } );






          share|improve this answer












          The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object. ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign ).



          But in your code Object.assign(user.lastUpdated, new Date()); what you are trying to do is join two values together. So it won't work.



          Try like this : Object.assign( user, { lastUpdated : new Date() } );







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 8:14









          Dananjaya AriyasenaDananjaya Ariyasena

          41010




          41010








          • 2




            It would "work" if user.lastUpdated is an object (we don't know whether it is or not). It copies the enumerable property names and values, but since a Date instance has no enumerable own properties, it does nothing. ;-) I think it would be better to use user.lastUpdated = new Date().toISOString() or whatever format the database expects.
            – RobG
            Nov 21 '18 at 8:27












          • you are right @RobG
            – darshan a n
            Nov 21 '18 at 8:29














          • 2




            It would "work" if user.lastUpdated is an object (we don't know whether it is or not). It copies the enumerable property names and values, but since a Date instance has no enumerable own properties, it does nothing. ;-) I think it would be better to use user.lastUpdated = new Date().toISOString() or whatever format the database expects.
            – RobG
            Nov 21 '18 at 8:27












          • you are right @RobG
            – darshan a n
            Nov 21 '18 at 8:29








          2




          2




          It would "work" if user.lastUpdated is an object (we don't know whether it is or not). It copies the enumerable property names and values, but since a Date instance has no enumerable own properties, it does nothing. ;-) I think it would be better to use user.lastUpdated = new Date().toISOString() or whatever format the database expects.
          – RobG
          Nov 21 '18 at 8:27






          It would "work" if user.lastUpdated is an object (we don't know whether it is or not). It copies the enumerable property names and values, but since a Date instance has no enumerable own properties, it does nothing. ;-) I think it would be better to use user.lastUpdated = new Date().toISOString() or whatever format the database expects.
          – RobG
          Nov 21 '18 at 8:27














          you are right @RobG
          – darshan a n
          Nov 21 '18 at 8:29




          you are right @RobG
          – darshan a n
          Nov 21 '18 at 8:29


















          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%2f53407626%2fassigning-a-date-object-to-update%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

          Ottavio Pratesi

          Tricia Helfer

          15 giugno