Node.js返回空数组

时间:2019-01-30 09:16:47

标签: javascript node.js mongodb mongoose

我正在用node.js编写代码。这很新,问题是猫鼬返回一个空数组。这段代码中某处肯定有一个错误,但我找不到它。有任何想法吗?

着装模式

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

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

从数据库获取礼服

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

1 个答案:

答案 0 :(得分:0)

尝试一下我的代码

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