My first attempt to create something using Angular + express + mongodb, so I'm probably wrong about that. Express is used to serve json. Angular, then take care of all the views, etc.
I use Mongoose to interact with Mongo.
I have the following database schema:
var categorySchema = new mongoose.Schema({
title: String,
retailers : [
{
title: String,
data: {
strapLine: String,
img: String ,
intro: String,
website: String,
address: String,
tel: String,
email: String
}
}
]
});
var Category = mongoose.model('Category', categorySchema);
and in Express, I have several ways to get data:
app.get('/data/categories', function(req, res) {
Category.find(function(err, data) {
if (err) return console.error(err);
res.json(data)
});
});
app.get('/data/retailer_list/:category', function(req, res) {
Category.findOne({ _id: req.params.category }, function(err, data) {
if (err) return console.error(err);
res.json(data)
});
});
The above jobs - I just have big problems trying to get into one retailer. I pass the category and retailer ID ... I tried all kinds of things - from searching in a category, and then from findOne from the contents inside ... but I just can't get it to work. I'm probably wrong ...
: findOne Subdocument Mongoose - , , .
app.get('/data/retailer_detail/:category/:id', function(req, res) {
Category.findOne({_id: req.params.category , 'retailers.$': 1}, function(err, data) {
console.log(data);
if (err) return console.error(err);
res.json(data)
});
});
,
Rob