猫鼬模式方法:错误-模型方法不是函数

塞尔吉奥·卡多佐(Sergio Cardoso)

我有以下两个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);
//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);

当我尝试检索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数据,当我用更多数据完成模型时,这将成为问题。我已经稍微研究了statics和方法,并尝试将其更改为statics,但是没有用。

我得到了SoilLabs,但不能确定为什么此时无法访问.SoilToLabJSON方法。我是否需要find()或以其他方式填充SoilLab?方法不正确吗?

塞尔吉奥·卡多佐(Sergio Cardoso)

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),

  };
};

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

猫鼬静态方法错误:不是函数

来自分类Dev

在猫鼬上存储模式的正确方法?

来自分类Dev

foo.find(...)。execFind不是函数-猫鼬模式模型

来自分类Dev

猫鼬对象方法不是函数

来自分类Dev

猫鼬填充方法的转换错误

来自分类Dev

如何在猫鼬模式方法中指定`this`

来自分类Dev

如何使用虚拟方法从猫鼬模式获取枚举值?

来自分类Dev

猫鼬“模式方法”回调不起作用

来自分类Dev

猫鼬模式方法返回未定义

来自分类Dev

扩展模式猫鼬中的继承方法“虚拟”无效

来自分类Dev

猫鼬-初始化嵌套模式的正确方法

来自分类Dev

如何在猫鼬模式方法中指定`this`

来自分类Dev

猫鼬“模式方法”回调不起作用

来自分类Dev

猫鼬vs方法在猫鼬

来自分类Dev

NodeJS TypeError 中的猫鼬模式:模式不是构造函数

来自分类Dev

猫鼬错误-升级后没有'toObject'方法

来自分类Dev

如何在猫鼬查询方法中返回错误?

来自分类Dev

用jsdoc记录猫鼬模型方法

来自分类Dev

在模型上存入猫鼬保存方法

来自分类Dev

猫鼬实例方法“ this”未引用模型

来自分类Dev

使用打字稿的猫鼬模式/模型

来自分类Dev

猫鼬模式尚未注册模型

来自分类Dev

TypeError:Dbschema(猫鼬模式)不是一个函数

来自分类Dev

TypeError:Test.findAll不是函数callinf猫鼬模式

来自分类Dev

猫鼬模式-必需的孩子,而不是父母

来自分类Dev

猫鼬模式-必需的孩子,而不是父母

来自分类Dev

猫鼬保存不是使用模式的功能

来自分类Dev

猫鼬和节点错误:无法调用未定义的方法“模型”

来自分类Dev

猫鼬的填充方法的MissingSchemaError