繁体   English   中英

执行查找查询时从 mongodb 获取空数组

[英]Getting an empty array from mongodb while executing the find query

提前致谢,为什么即使使用正确的架构名称,我也会从 Mongodb 获取一个空数组?

const Mongoose = require("mongoose");   
const studentSchema = Mongoose.Schema({
  _id: { type: Number, required: true },
  name: { type: String, required: true, max: 30 },
  department: { type: String, required: true,max: 30 },
  type: { type: String, required: true, min: 7, max: 30 }
});
module.exports = Mongoose.model("StudentInformation", studentSchema);
-----------------------------------------------------------------------
class  Service {
  static get() {
    const data = studentsModel.find({}).then((result) => {
        console.log(studentsModel)
      return result;
    });
    return data;
  }
}
----------------------------------------------------------------------

```
> db.getCollectionNames();
[ "StudentInformation", "students" ]
> db.StudentInformation.find();
{ "_id" : 1, "name" : "selva", "department" : "CSE", "type" : "regular" }
{ "_id" : 2, "name" : "ashik", "department" : "CSE", "type" : "regular" }
{ "_id" : 3, "name" : "praveen", "department" : "CSE", "type" : "parttime" }

您的 get() 函数中有错误的“返回”指令:

 static get() {
    const data = studentsModel.find({}).then((result) => {
        console.log(studentsModel)
      return result;
    });
    **return data;**
  }

这部分是异步的

studentsModel.find({}).then((result) => {

在执行 find 回调之前返回的数据为空/未定义

暂无
暂无

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

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