【问题标题】:nodejs mongodb , getting no result from dbnodejs mongodb,从数据库中没有得到任何结果
【发布时间】:2020-02-27 20:03:30
【问题描述】:

处理来自 MongoDb 的数据的问题

连接MongoDb成功

但是在 find 命令上,它应该返回一个空集合,但没有返回任何内容。

可能是什么问题,或者如何通过一些错误消息对其进行监控。

谢谢。

项目依赖

"dependencies": {
"body-parser": "^1.19.0",
"epxress": "0.0.1-security",
"express": "^4.17.1",
"nodemon": "^2.0.2"

}

猫鼬.js

const mongoose = require('mongoose');

mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/ListsDB', { useNewUrlParser: true, useUnifiedTopology: true}).then( ()=> {
    console.log("Connected to MongoDbB successfully ");
}).catch( (e) => {
    console.log("Error while attempting to connect to MongoDB");
    console.log(e);
});

mongoose.set('useCreateIndex', true);
mongoose.set('useFindAndModify', false);

module.exports = {
    mongoose
};

list.model.js

const mongoose = require('mongoose');

const ListSchema = new mongoose.Schema({
   title: {
       type: String,
       required: true,
       minlength: 1,
       trim: true
   }
})

const List = mongoose.model('List', ListSchema);

module.exports = { List }

app.js 不工作

const express = require('express');
const app = express();

const bodyParser = require('body-parser');

const { List, Task } = require('./db/models');

app.use(bodyParser.json());

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next()
  })

app.get('/lists', (req,res) => {
    // We want to return an array of all the lists in the database
    List.find({}).then( (err, lists)=> {
        res.send(lists)
    })
})

app.listen(3000, () => {
    console.log("Server is listening on port 3000");
})

【问题讨论】:

  • 你的意思是res.json(lists)
  • 不,我认为 res.send(lists) 是正确的。
  • 你在 app.js 中导入列表了吗?你检查过你的控制台错误吗?
  • @Shl 你能查一下吗?
  • @Juhil Somaiya 是的,是的,而且错误没有返回任何东西,怀疑它的 mongo 问题,但不知道如何监控它。

标签: javascript node.js mongodb express mongoose


【解决方案1】:

试试这个语法

   app.get('/lists', (req,res) => {
     List.find({}).then( lists => {
        res.send(lists)
     }).catch(err =>
        console.log(err)
     );
   })

【讨论】:

  • 您是否看到在 app.js 中导入了 List ?它将如何获得 List ?
  • @JuhilSomaiya 是的。
【解决方案2】:

您在 then 块中传递了多个参数。然后只接受标记为成功数据的单个参数。如果你想显示错误,你还必须在 catch 块中传递错误参数。

试试这个代码:

    const express = require('express');
    const bodyParser = require('body-parser');
    const List = require('./listModel');
    const mongoConnection = require('./mongoose');
    const app = express();

app.use(bodyParser.json({
    limit: '20mb',
    extended: true
}));
app.use(bodyParser.urlencoded({
    extended: true
}));

app.get('/lists', (req,res) => {
     List.find().then( lists => {
                res.send(lists)
          }).catch(err =>
                res.send(err)
          );
 });

app.listen(3100, () => {
    console.log("Server is listening on port 3100");
})

【讨论】:

  • 我已经试用了您的代码,该代码在此解决方案中运行良好。
  • 他的本地 mongodb @HardikPatel 可能有问题
  • 这似乎确实是一个 mongo 问题,任何想法如何验证/查找问题
  • 首先确保您的系统上安装了 mongoDB,如果已安装,请检查它是否已连接到您的应用程序。
  • 我认为您还没有在您的应用程序中连接您的 mongoDB。在您的 app.js 文件中添加以下行 const mongo = require('path to mongoconnection file');
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-27
  • 1970-01-01
  • 2019-10-02
  • 2013-11-25
  • 2012-09-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多