简体   繁体   English

nodejs mongodb ,从数据库中没有得到任何结果

[英]nodejs mongodb , getting no result from db

Issue with working on data from MongoDb处理来自 MongoDb 的数据的问题

Connecting to MongoDb successfully ,成功连接到 MongoDb

but on find command , it should return an empty collection , yet nothing is returned.但是在 find 命令上,它应该返回一个空集合,但没有返回任何内容。

what could be the issue , or how it could be monitored by some error messaging.可能是什么问题,或者如何通过一些错误消息来监控它。

Thanks.谢谢。

Project dependencies项目依赖

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

} }

mongoose.js猫鼬.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列表.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 Not Working 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");
})

Try using this syntax尝试使用此语法

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

You are passing multiple argument in then block.您正在 then 块中传递多个参数。 Then is only accept single argument that is mark as success data.然后只接受标记为成功数据的单个参数。 If you want to show errors also you have to pass in error argument in catch block.如果您还想显示错误,则必须在 catch 块中传入错误参数。

Try This code :试试这个代码:

    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");
})

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

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