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

'document' parameter is a function in post save middleware #13026

Closed
2 tasks done
MZPL opened this issue Feb 11, 2023 · 1 comment · Fixed by #13030
Closed
2 tasks done

'document' parameter is a function in post save middleware #13026

MZPL opened this issue Feb 11, 2023 · 1 comment · Fixed by #13030
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@MZPL
Copy link

MZPL commented Feb 11, 2023

Prerequisites

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

Mongoose version

6.9.1

Node.js version

19.5.0

MongoDB server version

6.0.4

Typescript version (if applicable)

No response

Description

Hi. When bulkSave() is called, it fires post save middleware, but for some reason, the doc parameter is a function, but typings indicate it should be a document. Using this works tho.
Apart from that, what is the proper way to get a document? Docs list these 2 ways, but I couldn't find the difference between them in the docs.

Steps to Reproduce

import mongoose from "mongoose";

mongoose.set("debug", true);
await mongoose.connect("mongodb://127.0.0.1:27017/mongooseissue");

const schema = new mongoose.Schema({ foo: Boolean, bar: Boolean });

schema.post("save", function (doc) {
    // 'doc' is typed as:
    // mongoose.Document<unknown, any, { foo?: boolean | undefined; bar?: boolean | undefined; }> & { foo?: boolean | undefined; bar?: boolean | undefined; } & { _id: mongoose.Types.ObjectId; }
    console.log("this:");
    console.log(this);
    console.log("doc:");
    console.log(doc);
})

const TestModel = mongoose.model("test", schema);

async function one() {
    const doc = new TestModel({ foo: true, bar: true });
    await doc.save();
    // 'this' and 'doc' are printed in post middleware, they're equal
}

async function two() {
    const docs = await TestModel.find({}).exec();
    docs.forEach(doc => {
        doc.foo = false;
    })
    await TestModel.bulkSave(docs);
    // 'this' is a document
    // 'doc' parameter is a function, why?
}

console.log("One");
await one();

console.log("Two");
await two();

Expected Behavior

The doc parameter is a document

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Feb 13, 2023
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

mongoose.set("debug", true);

async function run() {
  await mongoose.connect("mongodb://127.0.0.1:27017/mongooseissue");
  await mongoose.connection.dropDatabase();

  console.log("One");
  await one();

  console.log("Two");
  await two();
}

const schema = new mongoose.Schema({ foo: Boolean, bar: Boolean });

schema.post("save", function (doc) {
    // 'doc' is typed as:
    // mongoose.Document<unknown, any, { foo?: boolean | undefined; bar?: boolean | undefined; }> & { foo?: boolean | undefined; bar?: boolean | undefined; } & { _id: mongoose.Types.ObjectId; }
    console.log("this:");
    console.log(this);
    console.log("doc:");
    console.log(doc);
})

const TestModel = mongoose.model("test", schema);

async function one() {
    const doc = new TestModel({ foo: true, bar: true });
    await doc.save();
    // 'this' and 'doc' are printed in post middleware, they're equal
}

async function two() {
    const docs = await TestModel.find({}).exec();
    docs.forEach(doc => {
        doc.foo = false;
    })
    await TestModel.bulkSave(docs);
    // 'this' is a document
    // 'doc' parameter is a function, why?
}

run();

lpizzinidev added a commit to lpizzinidev/mongoose that referenced this issue Feb 14, 2023
vkarpov15 added a commit that referenced this issue Feb 15, 2023
fix(model): fixed post('save') callback parameter
@vkarpov15 vkarpov15 added this to the 6.9.2 milestone Feb 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants