我的数据没有正确保存在我的数据库中
My Data is not being Saved in my database correctly
当我将数据保存到数组中的数据库时它不起作用,但是当我只发送一个对象时它被正确存储。也许我的架构不正确?
这是我的架构
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;
当我将数据作为包含两个对象的数组发送时 none 例如,数据显示应该显示我输入的对象。当我发送这个时,服务器不会在数组中保存任何产品。
[{
"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 函数不return 模型数据。
不得不改变我的 post 方法
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;
当我将数据保存到数组中的数据库时它不起作用,但是当我只发送一个对象时它被正确存储。也许我的架构不正确?
这是我的架构
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;
当我将数据作为包含两个对象的数组发送时 none 例如,数据显示应该显示我输入的对象。当我发送这个时,服务器不会在数组中保存任何产品。
[{
"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 函数不return 模型数据。
不得不改变我的 post 方法
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;