Node.js MongoDb中的空响应对象

Node.js MongoDb中的空响应对象,node.js,express,mongoose,mongoose-schema,body-parser,Node.js,Express,Mongoose,Mongoose Schema,Body Parser,我试图获取mongo集合中的所有文档,但得到的却是空响应。我的数据库名是taskDb,它有一个名为item的集合,所有文档都存储在该集合中。我认为模式可能存在一些问题,但mongo是无模式的db,因此我无法找到解决方案 index.js var express = require('express'); var app = express(); var bodyParser = require('body-parser'); var mongoose = require('mongoose');

我试图获取mongo集合中的所有文档,但得到的却是空响应。我的数据库名是taskDb,它有一个名为item的集合,所有文档都存储在该集合中。我认为模式可能存在一些问题,但mongo是无模式的db,因此我无法找到解决方案

index.js

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var cors = require('cors');

app.use(bodyParser.json());
app.use(cors());

Items = require('./items.js');

mongoose.connect('mongodb://localhost/taskDb');
var db = mongoose.connection;

app.get("/",(req, res)=> {
    res.send('Visit /api/*****');
});

app.get("/api/items",(req, res)=> {
    Items.getItems(function(err, items){
        if(err){
            throw err;
        }
        console.log(res.json(items));
        res.json(items);
    });
});

// app.get("/api/matches",(req, res)=> {
//     Matches.getMatches(function(err, matches){
//         if(err){
//             throw err;
//         }
//         res.json(matches);
//     });
// });

// app.get("/api/deliveries/:playerName",(req, res)=> {
//     Deliveries.getPlayerStats(req.params.playerName ,function(err, deliveries){
//         if(err){
//             throw err;
//         }
//         res.json(deliveries);
//     });
// });

app.listen(3005,()=>{
   console.log('Listening on port 3005...');
});
item.js

var mongoose = require('mongoose');

var itemSchema = mongoose.Schema({
    _ID:{
        type: String,
        required: true
    },
    ITEM:{
        type: String,
        required: true
    },
    KEY:{
        type: Number,
        required: true
    },
    STATUS:{
        type: String,
        required: true
    }
});

var Item = module.exports = mongoose.model('item', itemSchema);

// module.exports.getPlayerStats = function (playerName, callback) {
//     Deliveries.aggregate([{$project:{_id: 1,batsman: 1 ,batsman_runs: 1, dismissal:{
//         $cond: [ { $eq: ["$player_dismissed", playerName ] }, 1, 0]
//     }}},{ $match: { batsman: playerName } },
//     { $group: {_id: "$batsman", total_runs: { $sum: "$batsman_runs" },total_dismissal: { $sum: "$dismissal"}}}
//     ], callback);
// }

module.exports.getItems = function (callback, limit) {
    Item.find(callback).limit(limit);
};

我在
getItems
函数中看到两个问题:

  • 未指定
    find()
    的条件。如果要读取所有记录,可以将其指定为空对象
  • 未将限制参数从请求处理程序传递到
    getItems
    函数。您可能需要将其默认为某个数字,或者处理无法通过
    limit
    的情况
  • getItems()
    修改为类似以下内容应该可以工作:

    module.exports.getItems = function (callback, limit) {
        Item.find({}, callback).limit(limit || 20); // Default limit to a feasible number
    };
    
    此外,如果要覆盖默认值,可以从请求处理程序将限制传递给
    getItems()
    函数:

    app.get("/api/items",(req, res)=> {
        Items.getItems(function(err, items){
            if(err){
                throw err;
            }
            console.log(res.json(items));
            return res.json(items);
        }, 50); // Pass limit
    });