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

Missing base.now #15269

Open
2 tasks done
iwanpark opened this issue Feb 22, 2025 · 3 comments
Open
2 tasks done

Missing base.now #15269

iwanpark opened this issue Feb 22, 2025 · 3 comments
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. Stale

Comments

@iwanpark
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.10.1

Node.js version

18.19.1

MongoDB server version

6.13.0

Typescript version (if applicable)

5.3.3

Description

I'm trying to update Mongoose from 7.6.8 to 8.10.1. I tested it on my staging server and got a strange error.

{
    "name": "TypeError",
    "message": "Cannot read properties of undefined (reading 'now')",
    "stack": "TypeError: Cannot read properties of undefined (reading 'now')\n    at setDocumentTimestamps (/tracer/node_modules/mongoose/lib/helpers/timestamps/setDocumentTimestamps.js:9:41)\n    at EmbeddedDocument.timestampsPreSave (/tracer/node_modules/mongoose/lib/helpers/timestamps/setupTimestamps.js:51:5)\n    at callMiddlewareFunction (/tracer/node_modules/kareem/index.js:628:27)\n    at EmbeddedDocument.next (/tracer/node_modules/kareem/index.js:93:7)\n    at _next (/tracer/node_modules/kareem/index.js:146:10)\n    at /tracer/node_modules/kareem/index.js:653:30\n    at processTicksAndRejections (node:internal/process/task_queues:77:11)"
}

I went to the problematic code and found this.

module.exports = function setDocumentTimestamps(doc, timestampOption, currentTime, createdAt, updatedAt) {
  const skipUpdatedAt = timestampOption != null && timestampOption.updatedAt === false;
  const skipCreatedAt = timestampOption != null && timestampOption.createdAt === false;

  const defaultTimestamp = currentTime != null ?
    currentTime() :
    doc.ownerDocument().constructor.base.now(); // Problem: base.now is undefined.

As I commented above, base.now is undefined. Could you fix it?

Steps to Reproduce

I tested it on my staging environment as already mentioned.

Expected Behavior

No response

@iwanpark
Copy link
Author

It looks like what is missing is not now but base itself. In other words, the base is undefined.

@vkarpov15
Copy link
Collaborator

Without a repro script, we're unlikely to be able to fix this. base is the Mongoose instance associated with the document's model constructor, so if base is undefined then .constructor might not be a Mongoose model.

Can you please modify the following script to demonstrate the issue you're seeing? The following script executes with no errors.

const mongoose = require('mongoose');

const connectionString = 'mongodb://localhost:27017/testdb';

const NestedSchema = new mongoose.Schema({ name: String }, { timestamps: true });
const ParentSchema = new mongoose.Schema({
  nested: NestedSchema
});

const ParentModel = mongoose.model('Parent', ParentSchema);

async function run() {
  await mongoose.connect(connectionString);

  // Create a document
  const doc = await ParentModel.create({ nested: { name: 'before' } });
  console.log('Created:', doc);

  // Update the nested document
  doc.nested.set({ name: 'after' });
  await doc.save(); // This should trigger the error
}

run();

@vkarpov15 vkarpov15 added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Feb 23, 2025
Copy link

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the Stale label Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. Stale
Projects
None yet
Development

No branches or pull requests

2 participants