【问题标题】:Mongoose Model functions not being recognized猫鼬模型功能未被识别
【发布时间】:2020-06-24 03:29:40
【问题描述】:

我已经设置了一个 NodeJS/Express 网络服务器,并且正在尝试将数据写入已经连接的数据库。我正在将 MongoDB 与 Mongoose 一起使用。这是我根据 Github 提交编写的用于保存到数据库中的测试 Schema。

// 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;

我已将其导入到一个文件中,该文件计算数据库中有多少此类 Commit 文档。

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

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

这将返回错误:_Commit__WEBPACK_IMPORTED_MODULE_2__.default.find is not a function。 我不明白我做错了什么。为什么它认为 Commit 模型无效?

【问题讨论】:

  • 你确定这是正确的路径"./Commit"

标签: node.js mongodb express mongoose ecmascript-6


【解决方案1】:

我想通了。不知何故,通过 React 应用程序的其余部分,该代码正在通过 App.js 反应文件运行。我不确定为什么。我将引用Commit.find()的代码移到服务器路由中,通过axios调用,一切正常。

【讨论】:

    【解决方案2】:

    您可以通过 npm install mongoose 使用 mongoose,并查看文档here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-04
      • 2020-02-05
      • 2021-10-02
      • 2016-11-18
      • 2017-10-05
      • 1970-01-01
      相关资源
      最近更新 更多