你好,我正在尝试连接一个mongoose数据库到我的项目使用Node js,Express和Mongoose,我遇到了一个奇怪的问题。成功连接到mongoose数据库并指定模型后,我的集合中似乎没有显示任何内容(即没有模式)。但是,当我使用"my new database“命令时,它会切换到该数据库。我已经找遍了所有地方,似乎找不到问题所在,我非常确定我的代码是正确的。下面是我的代码
导入模型并创建数据库:
const Item = require("./models/Item"),
mongoose.connect("mongodb://localhost:27017/SurfShareDB", {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("Your Database has been connected");
})
.catch((err) => {
console.log("ERROR", err.message);
}); 项目架构和模型:
const mongoose = require("mongoose");
const itemSchema = new mongoose.Schema({
title: String,
image: String,
description: String,
price: String,
created: { type: Date, default: Date.now },
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "user",
},
username: String,
},
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment",
},
],
});
module.exports = mongoose.model("Item", itemSchema);发布于 2020-08-31 07:00:24
在将某些内容写入集合中的文档、集合上的索引或集合的元数据之前,不会创建MongoDB中的集合。
https://stackoverflow.com/questions/63662706
复制相似问题