find({})返回一个空数组猫鼬 [英] find({}) returns an empty array mongoose

查看:55
本文介绍了find({})返回一个空数组猫鼬的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在model.js文件中的代码:

Here is my code in model.js file:

const mongoose = require("mongoose");
const con = require("./connection");
con();

const schoolNotices = mongoose.model("schoolNotices",
{
   title:{
       type: String
   },
   date:{
       type:String
   },
   details:{
       type:String
   }
});

module.exports = schoolNotices;

我已经将此代码导入了students.js文件

And I have imported this code into students.js file

router.route("/notices")
    .get((req,res)=>{
        schoolNotices.find({},(err,docs)=>{
            if(err){
                return console.log(err)
            }
            res.render("studentNotices",{title:"Notices",data:docs})
        })
        
    }).post(urlencodedParser,(req,res)=>{

    });

schoolNotices集合具有3个对象,如:

schoolNotices collection has 3 objects like:

{
    "_id" : ObjectId("5ffa80077245b1208057a4ca"),
    "title" : "annual sports day",
    "date" : "03-01-2021",
    "details" : "The point of using Lorem Ipsum is that it has a more-or-less normal distribution of 
    letters, as opposed to using 'Content here, content here', making it look like readable English. 
    Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model 
    text"
 }

find({},(err,docs))应该返回 docs 中所有对象的数组,但它返回一个空数组.

find({},(err,docs)) should return an array of all objects in docs but it returns an empty array.

推荐答案

您缺少使用 mongoose.Schema

const schoolNotices = mongoose.model("schoolNotices",
    new mongoose.Schema(
        {
            title:{
                type: String
            },
            date:{
                type:String
            },
            details:{
                type:String
            }
        },
        { collection: "schoolNotices" } // optional
    )
);

这篇关于find({})返回一个空数组猫鼬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆