Mongoose Schema.statics.func cannot access this

  node.js, question

schemas

  • book.js

var mongoose = require('mongoose');
 var Schema = mongoose.Schema;
 var ObjectId = Schema.Types.ObjectId;
 
 var BookSchema = new Schema({
 // some code
 });
 
 BookSchema.pre('save',(next) =>{
 console.log(this);  //this output here is {}
 // some code
 next();
 })
 BookSchema.statics = {
 fetch: (cb) => {
 console.log(this);  //this output here is {}
 // some code
 bracket
 bracket
 
 module.exports = BookSchema;

models

  • book.js

var mongoose = require('mongoose');
 var BookSchema = require('../schemas/book');
 var Book = mongoose.model('book',BookSchema);
 
 module.exports = Book;

routes

  • books.js

var Book = require('../models/book');
 mongoose.connect('mongodb://localhost/readbook');
 Book.fetch((err, books) => {
 // some code
 });

Both outputs in the code are{}
Please advise, O(∩_∩)O thank you

Just don’t use the arrow expression, the clipping indicates that the binding of this will be reset and mongoose will be affected.