简体   繁体   English

ZCCADDEDB567ABAE643E15DCF0974E503Z Model 功能无法识别

[英]Mongoose Model functions not being recognized

I have setup an NodeJS/Express webserver, and am attempting to write data into a DB that has already been connected.我已经设置了一个 NodeJS/Express 网络服务器,并试图将数据写入已经连接的数据库。 I am using MongoDB with Mongoose.我正在使用 MongoDB 和 Mongoose。 Here is a test Schema I wrote to save into the database, based off of a Github commit.这是我根据 Github 提交编写的保存到数据库中的测试模式。

// Commit.js
import mongoose from "mongoose";

const CommitSchema = mongoose.Schema({
  message: {
    required: true,
    type: String
  },
  url: {
    type: String,
    required: true
  },
  author: {
    type: Object,
    required: true,
    name: {
      type: String,
      required: true
    },
    email: {
      type: String,
      required: true
    },
    date: {
      type: Date,
      required: true
    }
  }
});

const CommitModel = mongoose.model("Commit", CommitSchema);

export default CommitModel;

I have imported this into a file that counts how many of these Commit documents there are in the DB.我已将其导入到一个文件中,该文件计算数据库中有多少这些 Commit 文档。

//lib.js
import Commit from "./Commit";

const test = async () => {
  console.log(await Commit.find());
}
test();

This returns the error: _Commit__WEBPACK_IMPORTED_MODULE_2__.default.find is not a function .这将返回错误: _Commit__WEBPACK_IMPORTED_MODULE_2__.default.find is not a function I don't understand what I am doing wrong.我不明白我做错了什么。 Why does it not see the Commit model as valid?为什么它看不到 Commit model 有效?

I figured it out.我想到了。 Somehow, through the rest of the React app, that code was being run through the App.js react file.不知何故,通过 React 应用程序的 rest,该代码正在通过 App.js 反应文件运行。 I am not sure why.我不确定为什么。 I moved the code referencing Commit.find() into a server route and called it through axios, and everything worked fine.我将引用Commit.find()的代码移到服务器路由中,并通过 axios 调用它,一切正常。

You can use mongoose via npm install mongoose, and see the docs here您可以通过 npm 安装 mongoose 使用 mongoose,并在此处查看文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM