Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moment.utc drops time part when used as mongoose default #5966

Open
ErikaA4 opened this issue Jan 17, 2022 · 2 comments
Open

moment.utc drops time part when used as mongoose default #5966

ErikaA4 opened this issue Jan 17, 2022 · 2 comments

Comments

@ErikaA4
Copy link

ErikaA4 commented Jan 17, 2022

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 bug.ts
{
  _id: new ObjectId("61e5471c712520a0f39ebbef"),
  birthday: 2022-01-17T00:00:00.000Z
}

Expected:

Erikas-Macbook:BUG erikaarnocki$ ts-node bug.ts
{
  _id: new ObjectId("61e5471c712520a0f39ebbef"),
  birthday: 2022-01-17T10:39:40.000Z
}

Desktop:

  • ts-node v8.10.2

Moment-specific environment

  • The time zone setting of the machine the code is running on
Erikas-Macbook:BUG erikaarnocki$ locale
LANG="en_GB.UTF-8"
LC_COLLATE="en_GB.UTF-8"
LC_CTYPE="en_GB.UTF-8"
LC_MESSAGES="en_GB.UTF-8"
LC_MONETARY="en_GB.UTF-8"
LC_NUMERIC="en_GB.UTF-8"
LC_TIME="en_GB.UTF-8"
LC_ALL=
  • The time and date at which the code was run
    Mon 17 Jan 2022 10:32:37 GMT
  • Other libraries in use (TypeScript, Immutable.js, etc)
{
  "devDependencies": {
    "@types/node": "^17.0.9",
    "cypress-wait-until": "^1.7.1"
  },
  "dependencies": {
    "moment": "^2.29.1",
    "mongoose": "^6.1.6",
    "typescript": "^4.5.4"
  }
}

Please run the following code in your environment and include the output:

console.log((new Date()).toString())
console.log((new Date()).toLocaleString())
console.log((new Date()).getTimezoneOffset())
console.log(navigator.userAgent)
console.log(moment.version)
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)
@ErikaA4 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
@ErikaA4
Copy link
Author

ErikaA4 commented Jan 17, 2022

Opened ticket to mongoose as well.
Automattic/mongoose#11233

@DiscoNova
Copy link

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 ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants