简体   繁体   English

Node.js返回空数组

[英]Node.js returning empty array

I am writing code with node.js. 我正在用node.js编写代码。 Quite new to this and the problem is that mongoose returns an empty array. 这很新,问题是猫鼬返回一个空数组。 There must be a mistake somewhere in this code, but I cannot find it. 这段代码中某处肯定有一个错误,但我找不到它。 Any ideas? 有任何想法吗?

Dresses schema 着装方案

var dressesSchema = mongoose.Schema({
    title:{
    type: String,
    required: true
},
description:{
    type: String,
    required: true
}
});

var Dress = module.exports = mongoose.model('Dress', dressesSchema);

Get dresses from database 从数据库获取礼服

module.exports.getDresses = function(callback, limit){
    Dress.find(callback).limit(limit);
};

Dress = require('./models/dress');

app.get('/api/dresses', function(req, res){
    Dress.getDresses(function(err, dresses){
        if(err){
            throw err;
        }
        res.json(dresses);
    });
});

Give a try to my code 尝试一下我的代码

module.exports.getDresses = async function(limit = 100){
    return await Dress.find(callback).limit(limit).exec();
};

Dress = require('./models/dress');

app.get('/api/dresses', function(req, res){
    let dresses = Dress.getDresses();
    res.json(dresses)
});

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

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