简体   繁体   English

ZCCADDEDB567ABAE643E15DCF0974E503Z 架构方法:错误 - model 方法不是 function

[英]Mongoose Schema method: Error - model method is not a function

I have two Mongoose model schemas as follows.我有两个 ZCCADCDEDB567ABAE643E15DCF0974E503Z model 模式如下。 The LabReport model contains an array of the referenced SoilLab model. LabReport model 包含一组引用的 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. SoilLab model 中有一个 static 方法,我用于 select 检索 LabReport 时显示哪些字段。

//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".当我尝试检索 LabReport 时,我得到“this.soilLabs.SoilToLabJSON 不是函数”。 This is how I'm trying to retrieve LabReport.这就是我尝试检索 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.如果我删除 LabReport.js 中的 .SoilToLabJSON 并调用 this.soilLabs,它可以工作,但会输出所有的 soilLabs 数据,当我用更多数据完成 model 时,这将成为一个问题。 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.我得到了要填充的土壤实验室,但不确定为什么此时无法访问 .SoilToLabJSON 方法。 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. labReport.toLabToJSON 正在传递一个数组,这导致了我的错误。 I simply edited the LabReport.js to the following to take the array and map it to SoilToLabJSON properly.我只是将 LabReport.js 编辑为以下内容,以将数组和 map 正确地转换为 SoilToLabJSON。

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

Changed the LabReportSchema.methods.toLabToJSON to:将 LabReportSchema.methods.toLabToJSON 更改为:

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

  };
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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