如何解决猫鼬模型静态错误

 收藏

I have the following model defined for my graphql API:

const mongoose = require("mongoose");

const Schema = mongoose.Schema;

const Post = Schema({
  _id: Schema.Types.ObjectId,
  title: { type: String, required: true },
  description: { type: String, required: true },
  content: { type: String, required: true },
  comments: { type: [String], required: true },
  tags: { type: [String], required: true },
  stats: { type: [Number], required: true },
  datePublished: { type: Date, required: true },
  dateUpdated: { type: Date, required: true }
});

// TODO: Implement methods here
// Post.statics.getPost = function(args) {
//   console.log(this.title);
//   return this.model
// };

Post.statics.getPost = function(args) {
  this.find({});
};
module.exports = mongoose.model("Post", Post, "posts");

I am willing to create a function in the model to fetch information from the database. But when I request the API for this function, I get the following response which is an error. I have also tried changing this to Post unsuccessfully.

{
  "errors": [
    {
      "message": "this.find is not a function",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "getPost"
      ]
    }
  ],
  "data": null
}

我怎么解决这个问题?

回复