【问题标题】:Error schema.find() is not a function when execute function执行函数时出现错误 schema.find() is not a function
【发布时间】:2022-11-05 23:59:42
【问题描述】:

当我尝试执行一个函数并使用 mongoose 在集合中查找值时,我得到错误 find() is not a function 我在函数之前在组件顶部的架构上使用 require

const resModel = require('../Backend/ResourcesModel')
    const getResources = () => {
        const cookie = document.cookie;
        const token = cookie && cookie.split('=')[1];
        const decode = JSON.parse(Buffer.from(token.split('.')[1], 'base64'));
        const para = {UserId:decode.user_id};
        console.log(para);
        resModel.find(para)
        .select('Food Marble Gold Solfour')
        .then(result => console.log(result))
    }

模型:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const ResourcesTemple = new Schema({
    UserId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true
    },
    Gold: {
        type: String,
        required: true
    },
    Solfour: {
        type: String,
        required: true
    },
    Marble: {
        type: String,
        required: true
    },
    Food: {
        type: String,
        required: true
    }
}, {versionKey: false});
const Resources = mongoose.model('Resources', ResourcesTemple);
module.exports = Resources;

错误: Uncaught TypeError: e.find is not a function

【问题讨论】:

  • 您可能还应该共享模型的代码,因为问题可能存在
  • 好的,无论如何我已经在这个模型中使用了 find() 在另一个用途中。
  • 我认为这是因为 require 架构没有任何结果,当我控制台记录它时,我得到 {} 为什么会发生这种情况?
  • 需要架构?
  • 是的,这个: const resModel = require('../Backend/ResourcesModel') 猫鼬模型。当我 console.log(resModel) 它返回 {} 我在 api 中使用这个模式并且它工作正常

标签: javascript reactjs function mongoose


【解决方案1】:

出现此问题的原因是在代码中发现了另一个错误,但没有注意到,因此架构结果为 null,因此它没有找到 find 函数。

【讨论】:

    猜你喜欢
    • 2015-11-01
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 2017-04-01
    相关资源
    最近更新 更多