【问题标题】:Mongoose filter sending any data猫鼬过滤器发送任何数据
【发布时间】:2021-09-08 11:59:01
【问题描述】:

所以我有这段代码

// GET request for pokedex page
app.get("/pokedex", function (req, res) {
    Pokemon.find(function (err, pokemon) {
        if (err) {
            console.log(err);
        } else {
            console.log(pokemon);
            res.render("pokedex", { pokemon: pokemon });
        }
    });
});

它把数据发送到前端就好了。数据也会被正确记录到控制台。问题是当我在回调之前添加{ information: { primaryType: "Grass" } }, 时,它不会发送任何数据。唯一记录的是空数组,但我正在尝试过滤数据。

我正在使用 JS、Node、Express 和 EJS。我也对其他过滤数据的方法持开放态度。

型号

[
  {
    information: {
      name: 'Bulbasaur',
      dexNumber: 1,
      primaryType: 'Grass',
      secondaryType: 'Poison',
      description: 'There is a plant seed on its back right from the day this Pokémon is born. The seed slowly grows larger.',
      devolvedForm: null,
      evolvedForm: 'Ivysaur'
    },
    stats: {
      health: 45,
      attack: 49,
      specialAttack: 65,
      defense: 49,
      specialDefense: 65,
      speed: 45
    },
    _id: 609cdab515102582ccf4e743,
    __v: 0
  },

【问题讨论】:

  • 你能展示你的模型吗?
  • @CuongLeNgoc 我已将其添加到我的帖子中

标签: javascript api express mongoose


【解决方案1】:

在这种情况下,您需要使用点符号进行过滤:

Pokemon.find({ "information.primaryType": "Grass" }...

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-21
  • 2019-12-29
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 2021-07-18
相关资源
最近更新 更多