【问题标题】:mongoose virtual populate infinite loop猫鼬虚拟填充无限循环
【发布时间】:2020-05-18 08:31:03
【问题描述】:

我正在尝试使用与其相关的教程填充标签,当我在查询上使用 .populate() 时它可以工作,但是当我直接在模型上执行时,我有一个无限循环。

这是我的代码:

Tag.js

const mongoose = require("mongoose");

const tagSchema = new mongoose.Schema(
  {
    name: {
      type: String,
      required: true,
      unique: true,
      trim: true
    }
  },
  {
    toObject: { virtuals: true },
    toJSON: { virtuals: true }
  }
);

tagSchema.virtual("tutorials", {
  ref: "Tutorial",
  foreignField: "tags",
  localField: "_id"
});

tagSchema.pre(/^find/, function(next) {
  // That's the code that causes an infinite loop
  this.populate({
    path: "tutorials",
    select: "-__v"
  });

  next();
});

const Tag = mongoose.model("Tag", tagSchema);

module.exports = Tag;

教程.js

const mongoose = require('mongoose');

const tutorialSchema = new mongoose.Schema({
  title: {
    type: String,
    required: true,
    unique: true,
    trim: true
  },
  tags: {
    type: [
      {
        type: mongoose.Schema.ObjectId,
        ref: 'Tag'
      }
    ]
  }
});

const Tutorial = mongoose.model('Tutorial', tutorialSchema);

module.exports = Tutorial;

我想知道是什么导致了无限循环,为什么它对查询起作用,但对模型不起作用?谢谢!

编辑

这是有效的代码

Tag.js

const mongoose = require("mongoose");

const tagSchema = new mongoose.Schema(
  {
    name: {
      type: String,
      required: true,
      unique: true,
      trim: true
    }
  },
  {
    toObject: { virtuals: true },
    toJSON: { virtuals: true }
  }
);

tagSchema.virtual("tutorials", {
  ref: "Tutorial",
  foreignField: "tags",
  localField: "_id"
});

const Tag = mongoose.model("Tag", tagSchema);

module.exports = Tag;

教程.js

const mongoose = require('mongoose');

const tutorialSchema = new mongoose.Schema({
  title: {
    type: String,
    required: true,
    unique: true,
    trim: true
  },
  tags: {
    type: [
      {
        type: mongoose.Schema.ObjectId,
        ref: 'Tag'
      }
    ]
  }
});

const Tutorial = mongoose.model('Tutorial', tutorialSchema);

module.exports = Tutorial;

TagController.js

const Tag = require('./../models/tagModel');

exports.getAllTags = async (req, res) => {
  try {
    const docs = await Tag.find().populate({
      path: 'tutorials',
      select: '-__v'
    });

    res.status(200).json({
      // some code
    });
  } catch(err) => {
    // some code
  }
});

【问题讨论】:

  • 你能添加这些代码吗? when I use .populate() on the query it works, but when I do it directly on the model I have an inifite loop. 当您更新问题时,请使用@SuleymanSah 标记我,以便通知我。
  • @SuleymanSah 感谢您的回答,我添加了您要求的代码
  • Sabrina 你能解释一下什么时候出现问题吗?显示导致错误的代码。我试图复制错误但没有成功。另外你能说出你用的是哪个猫鼬版本吗?
  • @SuleymanSah 在标签模型中间件中填充导致无限循环的标签
  • 你用什么版本的猫鼬?也是这个查询导致问题const docs = await Tag.find().populate({ path: 'tutorials', select: '-__v' });

标签: javascript node.js mongodb mongoose mongoose-populate


【解决方案1】:

我最近遇到了同样的问题,花了很长时间才弄清楚,在我的例子中,两个查询相互填充并导致无限循环。

【讨论】:

    猜你喜欢
    • 2021-01-09
    • 2017-04-04
    • 2018-12-23
    • 1970-01-01
    • 2015-07-13
    • 2016-10-10
    • 2015-11-19
    • 2021-09-19
    相关资源
    最近更新 更多