指定要更新的日期对象

问题描述 投票:1回答:1

我想知道如何在这种情况下分配日期对象,每当用户更改其详细信息时,我需要更新lastUpdate。

我也试过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
1个回答
2
投票

Object.assign()方法用于将所有可枚举自身属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)。

但是在你的代码Object.assign(user.lastUpdated, new Date());中你要做的是将两个值连接在一起。所以它不会起作用。

试试这样:Object.assign( user, { lastUpdated : new Date() } );

© www.soinside.com 2019 - 2024. All rights reserved.