简体   繁体   中英

Mongoose Schema method: Error - model method is not a function

I have two Mongoose model schemas as follows. The LabReport model contains an array of the referenced SoilLab model. There is a static method in the SoilLab model that I was using to select which fields to display when LabReport is retrieved.

//LabReport.js
var mongoose = require("mongoose");
var SoilLab = mongoose.model("SoilLab");

var LabReportSchema = new mongoose.Schema(
  {
    labFarm: { type: mongoose.Schema.Types.ObjectId, ref: "Farm" },
    testName: { type: String },
    soilLabs: [{ type: mongoose.Schema.Types.ObjectId, ref: "SoilLab" }],
  },
  { timestamps: true, usePushEach: true }
);

LabReportSchema.methods.toLabToJSON = function () {
  return {
    labReport_id: this._id,
    testName: this.testName,
    soilLabs: this.soilLabs.SoilToLabJSON(),

  };
};

mongoose.model("LabReport", LabReportSchema);
//SoilLab.js
var mongoose = require("mongoose");

var SoilLabSchema = new mongoose.Schema(
  {
    description: { type: String },
    sampleDate: { type: Date },
    source: { type: String },
  },
  { timestamps: true, usePushEach: true }
);

SoilLabSchema.methods.SoilToLabJSON = function () {
  return {
    description: this.description,
    sampleDate: this.sampleDate,
    source: this.source,
  };
};

mongoose.model("SoilLab", SoilLabSchema);

When I try to retrieve the LabReport, I get "this.soilLabs.SoilToLabJSON is not a function". This is how I'm trying to retrieve LabReport.

//labReports.js

...

    return Promise.all([
        LabReport.find()
          .populate("soilLabs")
          .exec(),
        LabReport.count(query).exec(),
        req.payload ? User.findById(req.payload.id) : null,
      ]).then(function (results) {
        var labReports = results[0];
        var labReportsCount = results[1];
        var user = results[2];
        return res.json({
          labReports: labReports.map(function (labReport) {
            return labReport.toLabToJSON(user);   //This cant find SoilToLabJSON 
          }),

If I remove the.SoilToLabJSON in LabReport.js and just call this.soilLabs, it works but outputs all of the soilLabs data which will become an issue when I have the model completed with more data. I have dug into statics vs methods a little and tried changing it to statics but it didn't work.

I get the soilLabs to populate but not sure why the.SoilToLabJSON method is inaccessible at this point. Do I need to find() or populate the soilLab differently? Is the method incorrect?

labReport.toLabToJSON is passing an array and that was causing the error for me. I simply edited the LabReport.js to the following to take the array and map it to SoilToLabJSON properly.

myTestSoilLabOutput = function (soilLabs) {
  var test = soilLabs.map(function (soilLab) {
    return soilLab.SoilToLabJSON();
  });
  return test;

Changed the LabReportSchema.methods.toLabToJSON to:

LabReportSchema.methods.toLabToJSON = function () {
  return {
    labReport_id: this._id,
    testName: this.testName,
    soilLabs: myTestSoilLabOutput(this.soilLabs),

  };
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM