Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Mongoose模式方法:错误模型方法不是函数_Javascript_Node.js_Mongodb_Express_Mongoose - Fatal编程技术网

Javascript Mongoose模式方法:错误模型方法不是函数

Javascript Mongoose模式方法:错误模型方法不是函数,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,我有两个Mongoose模型模式,如下所示。LabReport模型包含一组参考SoilLab模型。在SoilLab模型中有一个静态方法,我使用它来选择检索LabReport时要显示的字段 //LabReport.js var mongoose = require("mongoose"); var SoilLab = mongoose.model("SoilLab"); var LabReportSchema = new mongoose.Schema(

我有两个Mongoose模型模式,如下所示。LabReport模型包含一组参考SoilLab模型。在SoilLab模型中有一个静态方法,我使用它来选择检索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);
当我试图检索LabReport时,我得到“this.soilLabs.SoilToLabJSON不是一个函数”。这就是我试图检索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 
          }),
如果我删除LabReport.js中的.SoilToLabJSON并只调用this.soilLabs,它会工作,但会输出所有soilLabs数据,当我使用更多数据完成模型时,这将成为一个问题。我已经深入研究了静力学和方法,并尝试将其改为静力学,但没有成功


我得到了要填充的SOILLAB,但不确定为什么.SoilToLabJSON方法此时无法访问。我是否需要查找()或以不同方式填充soilLab?方法不正确吗?

labReport.toLabToJSON正在传递数组,这导致了我的错误。我只是将LabReport.js编辑为以下内容,以获取数组并将其正确映射到SoilToLabJSON

myTestSoilLabOutput = function (soilLabs) {
  var test = soilLabs.map(function (soilLab) {
    return soilLab.SoilToLabJSON();
  });
  return test;
将LabReportSchema.methods.toLabToJSON更改为:

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

  };
};

我注意到我的问题是soilLabs在这一点上是一个阵列。因此,函数无法支持数组,从而导致我的错误。我用“soilLabs:soilLabsArray(this.soilLabs)”更新了LabReportSchema.methods.toLabToJSON的方法,函数soilLabsArray使用.map并成功返回soilLab.SoilToLabJSON()。
LabReportSchema.methods.toLabToJSON = function () {
  return {
    labReport_id: this._id,
    testName: this.testName,
    soilLabs: myTestSoilLabOutput(this.soilLabs),

  };
};