【问题标题】:My Data is not being Saved in my database correctly我的数据未正确保存在我的数据库中
【发布时间】:2019-12-10 13:14:08
【问题描述】:

当我将数据保存到数组中的数据库时,它不起作用,但是当我只发送一个对象时,它被正确存储。也许我的架构不正确?

这是我的架构

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)
};
this is my post request

 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 newProduct = new models.productModel({
      colorC,
      sizeC,
      date,
      title,
      transactionID,
      count
    });
    const newPurchase = new models.cartModel({
      products: [newProduct.toJSON()]
    });

      await newProduct.save();
      await newPurchase.save()
      const products = newProduct;
    const purchase =  newPurchase + products;

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

module.exports = router;

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

[{

"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

}]

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

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

【问题讨论】:

  • 因为 toJson 不是你想的那样,它只是返回属性而不是模型数据。

标签: database mongodb mongoose schema mern


【解决方案1】:

toJson 函数不返回模型数据。

【讨论】:

  • 好的,但即使我删除了 tojson,我仍然无法以我想要的方式存储数据
  • 我不知道你为什么要先在两个地方存储产品
  • 是的,这没有意义我被建议在另一个问题上这样做,新购买不会保存新产品吗?
  • 是的,如果您保存模型购物车,那么您拥有所有产品,不需要其他产品模型,或者他们试图告诉您仅将产品的 ID 存储在购物车和产品本身在产品中
  • 您有 2 个选择,要么 1 个模型购物车,然后将产品存储在购物车内的字段 products 中,要么 2 个模型,然后将产品存储在自己的模型中,然后在字段中使用 Id购物车内的产品。
【解决方案2】:

不得不改变我的发帖方式

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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多