我想创建订单,但是由于订单猫鼬模式中的产品引用,所以无法创建订单,因此在订单服务中要通过什么功能才能创建订单。这是我写的代码
订购api控制器
exports.orders_create_order = (req, res, next) => {
Product.findById(req.body.productId)
.then(product => {
if (!product) {
return res.status(404).json({
message: "Product not found"
});
}
const order = new Order({
_id: mongoose.Types.ObjectId(),
quantity: req.body.quantity,
product: req.body.productId,
orderDate: req.body.orderDate
});
return order.save();
})
.then(result => {
console.log(result);
res.status(201).json({
message: "Order stored",
createdOrder: {
_id: result._id,
product: result.product,
quantity: result.quantity,
orderDate: result.orderDate
}
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
};
order.service.ts
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { HttpModule } from '@angular/http';
import { ActivatedRoute } from '@angular/router';
import 'rxjs/add/operator/map';
import { tokenNotExpired } from 'angular2-jwt';
@Injectable()
export class OrderService{
product:any;
constructor(
private http: Http,
private route: ActivatedRoute) {
}
getOrders() {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.get('orders', {headers:headers})
.map(res =>res.json());
}
createOrder(product:object) {
let headers = new Headers();
headers.append('Content-Type','application/json');
headers.append('Accept','application/json');
return this.http.post('orders',product,{headers:headers})
.map(res => res.json());
}
}
创建订单功能
createorder() {
this.orderservice.createOrder(this.product).subscribe(data=>{
this.product=data.product;
});
}
我无法获得订单控制器创建订单所需的服务中传递的功能。订单架构中以产品为参考,并下达了数量和日期。