You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
When we create a date field and default it to moment.utc, when it gets populated the date part of the timestamp is correct, but the time part gets lost and defaulted to midnight.
To Reproduce
Steps to reproduce the behavior:
Run the attached script.
const mongoose = require('mongoose');
const moment = require('moment');
main().catch(err => console.log(err)).then();
async function main() {
await mongoose.connect('mongodb://localhost:27017/test');
const kittySchema = new mongoose.Schema({
birthday: {default: moment.utc, type: Date}
});
const Kitten = mongoose.model('Kitten', kittySchema);
const cat = new Kitten({});
console.log(cat);
}
Expected behavior
Timestamp to be set to now with hour, minute and second correctly displayed (not zero).
Got this:
Erikas-Macbook:BUG erikaarnocki$ ts-node
> console.log((new Date()).toString())
Mon Jan 17 2022 10:33:56 GMT+0000 (Greenwich Mean Time)
undefined
> console.log((new Date()).toLocaleString())
17/01/2022, 10:33:56
undefined
> console.log((new Date()).getTimezoneOffset())
0
undefined
> console.log(navigator.userAgent)
/Users/erikaarnocki/BUG/[eval].ts:1
console.log(navigator.userAgent);
^
Uncaught ReferenceError: navigator is not defined
at /Users/erikaarnocki/BUG/[eval].ts:1:13
at Script.runInThisContext (vm.js:133:18)
at exec (/usr/local/lib/node_modules/ts-node/src/bin.ts:347:17)
at /usr/local/lib/node_modules/ts-node/src/bin.ts:337:27
at Array.reduce (<anonymous>)
at _eval (/usr/local/lib/node_modules/ts-node/src/bin.ts:336:18)
at REPLServer.replEval (/usr/local/lib/node_modules/ts-node/src/bin.ts:387:16)
at bound (domain.js:413:15)
at REPLServer.runBound [as eval] (domain.js:424:12)
at REPLServer.onLine (repl.js:817:10)
The text was updated successfully, but these errors were encountered:
ErikaA4
changed the title
moment.utc drops time part when used default
moment.utc drops time part when used as mongoose default
Jan 17, 2022
Whenever You use a temporal component in Your tests (like the "current date/time", which moment.utc()-method internally uses) ... You should be prepared to mock the native Date-implementation of JavaScript to return the expected Date/Time whenever called in Your tests. Otherwise Your tests will fail tomorrow, a minute from now, etc.
This is not an issue with Moment and it is not an issue with mongoose. You just need a bit of prerun setup on Your test ;)
Describe the bug
When we create a date field and default it to moment.utc, when it gets populated the date part of the timestamp is correct, but the time part gets lost and defaulted to midnight.
To Reproduce
Steps to reproduce the behavior:
Run the attached script.
Expected behavior
Timestamp to be set to now with hour, minute and second correctly displayed (not zero).
Got this:
Expected:
Desktop:
Moment-specific environment
Mon 17 Jan 2022 10:32:37 GMT
Please run the following code in your environment and include the output:
The text was updated successfully, but these errors were encountered: