【问题标题】:POST array of products details发布产品详细信息数组
【发布时间】:2020-08-18 21:00:34
【问题描述】:

我想知道是否可以发布一系列产品 ID 并显示它们的详细信息,因为正如您在下面看到的那样,只显示第一个产品。

邮递员:

{
	"productId":["5e1c9fc052b6b1b6b8fee292", "5e5a6309d07a33280ce8c49d"],
	"firstName":"hhhhh",
    "lastName": "hhhh",
    "email":  "tal",
    "phone": "123",
    "address": "tal"
}

邮递员回复:

{
    "message": "Order was Created",
    "order": {
        "product": [
            {
                "_id": "5e1c9fc052b6b1b6b8fee292",
                "product_Name": "Soccer Ball",
                "product_Description": "Soccer Ball",
                "product_Price": 20,
                "product_Img": "./assets/accessories/soccerball.jpg",
                "id": 0,
                "product_Quantity": 1,
                "type": "accessories"
            }
        ],
        "_id": "5eb005629fe7ab3f04a9c9a1",
        "email": "tal",
        "firstName": "hhhhh",
        "lastName": "hhhh",
        "phone": 123,
        "address": "tal",
        "createdAt": "2020-05-04T12:06:58.174Z",
        "__v": 0
    },
    "request": {
        "type": "GET",
        "order_url": "http://localhost:5000/order/5eb005629fe7ab3f04a9c9a1"
    }
}

订单架构:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const Product = require('../models/Product').schema

let Order = new Schema ({
    _id: mongoose.Schema.Types.ObjectId,
    firstName: {type: String},
    lastName: {type: String},
    email:  {type: String},
    phone: {type: Number},
    address: {type: String},
    product: [{type:Product, ref: 'Product', required: true}],
    // product: [{type:mongoose.Schema.Types.ObjectId, ref: 'Product', 
     required: true}],
    product_Name:[{type:String,required: true}],
    // product_Description:{type:Product.product_Description},
    // product_Price:{type:Product.product_Price},
    // id:{type:Product.id},
    createdAt:  {type: Date, default: Date.now}
},
{
    collection: 'orders'
}
);

module.exports = mongoose.model('Order', Order);

订单路由:

//create order
orderRoute.route('/').post((req, res) =>{
    Product.findById(req.body.productId)
    // .select('product')
    .populate('product')
    .then(product => {
        if(!product){
            return res.status(404).json({
                message: "product not found"
            });
        }
        const order = new OrderDetails({
            _id: new mongoose.Types.ObjectId(),
            product:[product],
            // product:req.body.productId,
            email: req.body.email,
            firstName:req.body.firstName,
            lastName: req.body.lastName,
            phone: req.body.phone,
            address: req.body.address,
        });
        return order.save()
        })
 
        .then(result => {
            console.log(result);
            
            return res.status(200).json({
                
                message: "Order was Created",
                
                order:result,
                request:{
                    type:"GET",
                    order_url: "http://localhost:5000/order/" + result._id
                }
            });
        })
        .catch(err => {
            console.log(err);
            res.status(500).json({
                error:err.message
            });
        });
    
    });   

在订单路由中,使用 product:req.body.productId 而不是 product:[product] 响应是 ID 数组,但没有详细信息。 这是为什么呢?

    "message": "Order was Created",
    "order": {
        "product": [
            "5e1c9fc052b6b1b6b8fee292",
            "5e5a6309d07a33280ce8c49d"
        ],

非常感谢!

【问题讨论】:

    标签: javascript node.js post mongoose routes


    【解决方案1】:
    orderRoute.route('/').post((req, res) => {
      const productIDs = req.body.productId;
      const order = new OrderDetails({
        product: productIDs,
        email: req.body.email,
        firstName: req.body.firstName,
        lastName: req.body.lastName,
        phone: req.body.phone,
        address: req.body.address,
      });
      return order.save()
        .then(result => {
          result.populate("product").then(order => {
            console.log(order);
            return res.status(200).json({
              message: "Order was Created",
              order,
              request: {
                type: "GET",
                order_url: "http://localhost:5000/order/" + result._id
              }
            });
          }).catch(err => {
            console.log(err);
            res.status(500).json({
              error: err.message
            });
          });
        })
        .catch(err => {
          console.log(err);
          res.status(500).json({
            error: err.message
          });
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 2019-11-11
      • 2021-02-22
      • 2022-11-02
      • 2012-05-28
      • 2013-04-19
      相关资源
      最近更新 更多