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
schema.pre('save' for document not working since 6.1.5 #11257
Comments
const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
name: String
});
testSchema.pre('save', { document: true, query: false } ,function() {
console.log('saving ', this);
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
const entry = new Test({name: 'Test'});
await entry.save();
}
run(); |
well I checked printed message, but then this.isNew and this.isModified emit error since 6.1.5
|
This is maybe just lack of typescript typings I've temporarily solved this problem like this
and this works fine since "this" is a mongoose.document type after all |
import { Schema, model, connect, connection } from 'mongoose';
// 1. Create an interface representing a document in MongoDB.
interface User {
name: string;
email: string;
avatar?: string;
}
// 2. Create a Schema corresponding to the document interface.
const schema = new Schema<User>({
name: { type: String, required: true },
email: { type: String, required: true },
avatar: String
});
schema.pre('save', {document: true, query: false}, function() {
console.log('saving', this)
})
// 3. Create a Model.
const UserModel = model<User>('User', schema);
run().catch(err => console.log(err));
async function run(): Promise<void> {
// 4. Connect to MongoDB
await connect('mongodb://localhost:27017/test');
await connection.dropDatabase();
const doc = new UserModel({
name: 'Bill',
email: 'bill@initech.com',
avatar: 'https://i.imgur.com/dM7Thhn.png'
});
await doc.save();
} |
use this.isNew |
@alanpurple can you please clarify what your most recent comment means? |
@vkarpov15 check this error |
import { Schema, model, connect, connection, Query, Document } from 'mongoose';
// 1. Create an interface representing a document in MongoDB.
interface User {
name: string;
email: string;
avatar?: string;
}
// 2. Create a Schema corresponding to the document interface.
const schema = new Schema<User>({
name: { type: String, required: true },
email: { type: String, required: true },
avatar: String
});
schema.pre('save', {document: true, query: false}, function() {
console.log('saving', this);
console.log('Is this a Document?', this instanceof Document, 'Is this a Query?', this instanceof Query);
console.log(this.$__); // says property $__ does not exist on type Query<any,any, {}, any> on 6.1.5 and up, returns a value below.
});
// 3. Create a Model.
const UserModel = model<User>('User', schema);
run().catch(err => console.log(err));
async function run(): Promise<void> {
// 4. Connect to MongoDB
await connect('mongodb://localhost:27017/test');
await connection.dropDatabase();
const doc = new UserModel({
name: 'Bill',
email: 'bill@initech.com',
avatar: 'https://i.imgur.com/dM7Thhn.png'
});
await doc.save();
} |
Yes, I left a note in the script for vkarpov15 when he comes to check it out. |
sorry for my misunderstanding |
this happens again with 6.7.2 only with 'updateOne'
|
… `{ document: true, query: false }` Fix #11257
fix(types): correctly infer `this` when using `pre('updateOne')` with `{ document: true, query: false }`
Do you want to request a feature or report a bug?
I can't decide, bug or not?
What is the current behavior?
is not working since 6.1.5
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
"this" should be the document before being saved
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
nodejs 16.13.2 x64
mongoose 6.1.4(working) 6.1.5~7(not working)
mongodb 5.0.5 x64
6.1.4 since I rollbacked
The text was updated successfully, but these errors were encountered: