问题
My Data is not being saved correctly
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)
};
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 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
}
回答1:
mongoose schema does not have constructor
new models.ProductSchema({ ...data })
will give you an error.
to do so you have to export models of the schema
module.exports = {
cartModel: mongoose.model('Cart', CartSchema),
productModel: mongoose.model('Cart', ProductSchema),
}
now you can use models constructor to achieve what you want
const newProduct = new models.cartModel({
colorC,
sizeC,
date,
title,
transactionID,
count
})
and then you can use your product to store into wherever you want
const newPurchase = new models.CartSchema({
products: [newProduct.toJSON()]
});
and yes you have to save both.
newProduct.save();
newPurchase.save();
回答2:
You don't need the
productModel: mongoose.model('Product', ProductSchema)
You can just just save the full cart json:
const newPurchase = new models.cartModel({
products:[{
"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
}]
});
await newPurchase.save()
(obviously you have to take the values dynamically from the request body)
回答3:
Had to restructure this file
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;
来源:https://stackoverflow.com/questions/57300657/i-am-trying-to-create-a-new-cart-schema-with-a-request