我有一组具有以下架构的对象:
var Meetup = new Schema({
name: String,
text:String,
});
我想得到所有名字都包含字符串的聚会。
这是我的api:
module.exports.list = function (req, res) {
Meetup.find({}, function (err, results) {
res.json(results);
});
}
在我的角度控制器中我有:
var Meetup = $resource('/api/meetups');
$scope.meetups = []
Meetup.query(function (results) {
$scope.meetups = results;
});
任何人都可以提供帮助
答案 0 :(得分:2)
查询特定字段
ModelName.find({fieldName: value}, function (err, results) {
//...
});
因此,对于您的案例查询将如下:
exports.list = function (req, res) {
Meetup.find({name: req.query.name}, function (err, results) {
res.json(results);
});
};
和角度控制器一样
var Meetup = $resource('/api/meetups', {}, {
query: {method: 'get', isArray: true}
});
$scope.meetups = []
Meetup.query({name: 'yourName'}).$promise.then(function(results) {
// console.log(results);
$scope.meetups = results;
}, function(error) {
// console.log(error);
$scope.meetups = [];
});