我正在用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);
});
});
答案 0 :(得分:2)
示例如何通过猫鼬使用查找:
// named john and at least 18
MyModel.find({ name: 'john', age: { $gte: 18 }});
// executes immediately, passing results to callback
MyModel.find({ name: 'john', age: { $gte: 18 }}, function (err, docs) {});
// name LIKE john and only selecting the "name" and "friends" fields, executing immediately
MyModel.find({ name: /john/i }, 'name friends', function (err, docs) { })
// passing options
MyModel.find({ name: /john/i }, null, { skip: 10 })
// passing options and executing immediately
MyModel.find({ name: /john/i }, null, { skip: 10 }, function (err, docs) {});
// executing a query explicitly
var query = MyModel.find({ name: /john/i }, null, { skip: 10 })
query.exec(function (err, docs) {});
// using the promise returned from executing a query
var query = MyModel.find({ name: /john/i }, null, { skip: 10 });
var promise = query.exec();
promise.addBack(function (err, docs) {});
取自link
答案 1 :(得分:0)
尝试一下:
着装模式:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const Dress= new Schema({
title:{
type: String,
required: true
},
description:{
type: String,
required: true
},
price:{
type: String
},
stock:{
type: String
},
location:{
country:{
type: String
},
city:{
type: String
},
street:{
type: String
},
lon:{
type: String
},
lat:{
type: String
}
}
});
module.exports = mongoose.model("dress", Dress);
从数据库获取礼服:
const {Dress} = require('./models/dress');
Dress.find().then(result => {
console.log(result);
});