我正在用Express和Mongoose构建一个api(前面的Backbone)。我有一个Mongoose User模型,其中包含一个名为“orders”的数组。我需要设置一个按ID读取单个订单的创建方法。
当我导航到:
http://localhost:3000/test/
我为登录用户提供以下信息:
{
"__v": 0,
"_id": "537d09a1fe47a00000c54514",
"kittenType": "Grumpy",
"local": {
"petname": "Smeagol",
"password": "$2a$08$X4sF5UmYZ3/2cxfRzpPcq.pphYFRKcb.6xBGupdUyUMgWJlFSr/uq",
"email": "julie@gmail.com"
},
"orders": [
{
"title": "Big Max Fountain",
"description": "Large capacity drinking fountain",
"quantity": "2",
"price": 500,
"_id": "53837e9e681808e6ea9f9ca4",
"modified": "2014-05-28T23:49:10.232Z"
},
{
"title": "Lotus Fountain",
"description": "Tranquil pools of water",
"quantity": "1",
"price": 1000,
"_id": "53867762ff514df026b608fa",
"modified": "2014-05-28T23:55:16.263Z"
}
]
}
当我导航到:
http://localhost:3000/test/orders
我发送登录用户的订单列表(这给了我订单数组:
app.get('/test/orders', function(req, res) {
User.findOne({'_id': req.user.id }, function(err, user) {
if (err)
return done(err);
if (user) {
res.send(user.orders);
}
});
});
如何按ID发送每个订单?
app.get('/test/orders/:id', function(req, res) {
User.findOne({'_id': req.user.id }, function(err, user) {
if (err)
return done(err);
if (user) {
//send the order by id here thru the url
}
});
});
在回复之后添加:
var userSchema = mongoose.Schema({
user : {
type: mongoose.Schema.ObjectId,
ref: 'User'
},
orders: [{
title: String,
description: String,
quantity : String,
price : Number,
modified: { type: Date, default: Date.now }
}],
signup: [{
name: String,
courseDay: String,
time: String,
location: String,
modified: { type: Date, default: Date.now }
}],
kittenType : String,
profilePhoto : String,
profilePage : String,
local : {
email : String,
password : String,
petname : String,
path : String,
}
路线:
app.get('/test', function(req,res) {
res.send(res.locals.user);
});
app.get('/test/orders', function(req, res) {
User.findOne({'_id': req.user.id }, function(err, user) {
if (err)
return done(err);
if (user) {
res.send(user.orders);
}
});
});
app.post('/api/orders', isLoggedIn, function (req, res){
User.findOne({'_id': req.user.id }, function(err, user) {
if (err)
return done(err);
if (user) {
user.orders.quantity = req.body.quantity;
user.orders.description = req.body.description;
user.orders.title = req.body.title;
user.orders.price = req.body.price;
user.orders.modified = req.body.modified;
user.update({$push: { "orders" :
{ title: user.orders.title,
description: user.orders.description,
quantity: user.orders.quantity,
price: user.orders.price,
modified: user.orders.modified
}
}},{safe:true, upsert:true},function(err){
if(err){
console.log(err);
} else{
console.log("Successfully added" + user.orders);
}
});
console.log('located a user');
}
});
});
工作方法:
app.get('/test/orders/:id', function(req, res) {
User.findOne({'_id': req.user.id }, function(err, user) {
if (err)
return done(err);
if (user) {
console.log(user.orders);
var order = user.orders.filter(function(e){ return e._id == req.params.id })[0]
console.log(order);
res.send(order);
}
});
});
答案 0 :(得分:1)
我认为在这种情况下你不需要找到用户。足以找到具有适当条件的Order
:
app.get('/test/orders/:id', function(req, res) {
Order.findOne({'_id': req.params.id, 'user_id': req.user.id }, function(err, order) {
if (err)
return done(err);
if (order) {
res.send(order);
}
});
});
但是您应该记录req
以确保使用正确的id
。这也取决于你的路线,你没有公开。
或者,如果您需要查找User
模型,则可以使用过滤方法。代码与第一种方法几乎相同:
app.get('/test/orders/:id', function(req, res) {
User.findOne({'_id': req.user.id }, function(err, user) {
if (err)
return done(err);
if (user) {
console.log(user.orders); // returns an array
// console.log(req.id); // to be sure that it returns proper order id
// perhaps it could be next
console.log(req.params.id);
var order_id = user.orders.filter(function(e){ return e == req.params.id })[0]
// then find this order
Order.findOne({'_id': order_id }, function(err1, order) {
if (err1)
return done(err1);
if (order) {
res.send(order);
}
});
}
});
});