我有一个模型,我存储了一些“视图”,但在我的代码的某些部分,我只想获得那些类型值等于home的那些。
我需要它由Ajax完成。
下面的代码已经获得了数据库的所有元素,但我只需要那些种类属性等于 home 的代码:
'onTabShow':function (tab, navigation, index) {
console.log(index);
if (index == 1){
$.ajax({
type: 'GET',
url: '/userpanel/webapi/viewlist',
dataFilter:(kind,home),//this doesnt work, there has to be a way to properly do it
}).done(function(response) {
console.log(response);
})
}
}
});
这是型号代码
var TIPOS_MEDIA = 'video serie picture sound live home'.split(' ');
const View = new Schema({
viewname: String,
component: String,
componenturl: String,
screenshot: String,
price: Number,
kind: { type: String, enum: TIPOS_MEDIA, default: 'video' },
options: [{ type: Schema.Types.ObjectId, ref: 'Option' }],
platform: { type: Schema.Types.ObjectId, ref: 'Platform' },
});
ViewModel = mongoose.model('View', View);
module.exports = ViewMode
如果需要,这是webapi代码
router.get('/userpanel/webapi/viewlist', function(req, res) {
View.find({})
.populate("options",'optionname')
.populate("platform")
.exec(function(err, views) {
if (err) {
res.send(err);
} else {
var _views = [];
for (var i = views.length - 1; i >= 0; i--) {
var view = {
_id:views[i]._id,
viewname:views[i].viewname,
component:views[i].component,
componenturl:views[i].componenturl,
screenshot:views[i].screenshot,
price:views[i].price,
kind:views[i].kind,
platform:views[i].platform.platformname,
};
var optionnames = "";
for (var i2 = views[i].options.length - 1; i2 >= 0; i2--) {
optionnames = optionnames + views[i].options[i2].optionname + ","
}
view.optionnames = optionnames;
_views.push(view);
}
res.json({
data:_views
});
}
})
});