我正在尝试使用请求创建新的购物车模式 [英] I am trying to create a new Cart schema with a request

查看:54
本文介绍了我正在尝试使用请求创建新的购物车模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据没有正确保存

这是我的Schema

This is my SChema

  const mongoose = require('mongoose');

const ProductSchema = mongoose.Schema({
  colorC: String,
  sizeC: String,
  date: Date,
  title: String,
  transactionID: Number,
  count: Number
});

const CartSchema = mongoose.Schema({
  products: [ProductSchema]
});

const Cart = mongoose.model('Cart', CartSchema);

module.exports = {
  cartModel: mongoose.model('Cart', CartSchema),
  productModel: mongoose.model('Product', ProductSchema)
};

这是我的发帖请求

const express = require('express');
const Cart = require('../models/Cart');
const models = require('../models/Cart');
const router = express.Router();

router.post('/', async (req, res) => {
  const { colorC, sizeC, date, title, transactionID, count } = req.body;
  try {
    const newPurchase = new models.cartModel({
      products: [
        {
          title,
          colorC,
          sizeC,
          count,
          date,
          transactionID
        }
      ]
    });

    const purchase = await newPurchase.save();

    res.json(purchase);
  } catch (err) {
    console.error(err.message);
    res.status(500).send('Server Error');
  }
});

module.exports = router;

当我将数据作为包含两个对象的数组发送时,没有数据显示出来,例如这应该显示我输入的对象. 当我发送此消息时,服务器不会在阵列中保存任何产品.

When i send the data as an array containing two objects none of the data shows up for instance this is supposed to show the object i input. When i send this the server does not save any products in the array.

这就是我要发送到服务器的内容

This is what i am sending to the server

[{

"colorC": null,
"count": 1,
"date": "Mon Jul 29 2019 02:08:07 GMT-0400 (Eastern Daylight Time)",
"sizeC": "Small",
"title": "COMME DES GARCONS TEE",
"transactionID": 1564380487732

},{

"colorC": null,
"count": 1,
"date": "Mon Jul 29 2019 02:08:07 GMT-0400 (Eastern Daylight Time)",
"sizeC": "Small",
"title": "COMME DES GARCONS TEE",
"transactionID": 1564380487732

}]

这是保存在数据库中的数据

This is the data saved in the Database

{
    "_id":{"$oid":"5d42ab9f4ec81021f0136e95"},
    "products":[{"_id":{"$oid":"5d42ab9f4ec81021f0136e94"}}]
    ,"__v":{"$numberInt":"0"}
    }

我希望数据看起来像这样

I am expecting the data to look like this

{
    "_id": "5d439bc758a1f004b876bac3",
    "products": [
        {
            "_id": "5d439bc758a1f004b876bac4",
            "title": "COMME DES GARCONS TEE",
            "colorC": null,
            "sizeC": "Small",
            "count": 1,
            "date": "2019-07-29T06:08:07.000Z",
            "transactionID": 1564380487732
        }
    ],
    "__v": 0
}

推荐答案

必须重新构建此文件

 const express = require("express");
    const models = require("../models/Cart");
    const router = express.Router();

    router.post("/", (req, res) => {
      const newPurchase = new models.cartModel({
        products: req.body.map(element => {
          const { colorC, sizeC, date, title, transactionID, count } = element;
          return { colorC, sizeC, date, title, transactionID, count };
        })
      });

      newPurchase
        .save()
        .then(purchase => res.json(purchase))
        .catch(err => {
          console.error(err.message);
          res.status(500).send("Server Error");
        });
    });

    module.exports = router;

这篇关于我正在尝试使用请求创建新的购物车模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆