[node.js]Angular MongoDB 在为用户添加新购物车时出现问题
· 收录于 2024-01-06 16:19:06 · source URL
问题详情
如果不存在,我正在尝试将产品添加到用户的购物车中。 我尝试自己解决这个问题,但我仍然得到相同的结果。
当我尝试将用户ID添加到购物车并且该用户的购物车没有退出时,我可以创建购物车,但未显示任何属性,仅创建了对象ID。 根据我的理解,应该是从所需的机构获取数据。 我认为这必须与我的架构有关。 我非常感谢任何帮助为我指明正确的方向。我已经研究了,但找不到解决方案,所以我正在联系,因为我有一段时间在做这件事。
This is what I get:
[
{
_id: ObjectId("658bfdd2e06b20c39f8816f8"),
userId: ObjectId("6588baa65a346036b4479398"),
product: [ { _id: ObjectId("658bfdd2e06b20c39f8816f9") } ],
active: true,
modifiedOn: ISODate("2023-12-27T10:34:58.900Z"),
createdAt: ISODate("2023-12-27T10:34:58.932Z"),
updatedAt: ISODate("2023-12-27T10:34:58.932Z"),
__v: 0
}
]
This is my request
product: {_id: "651352899db20fb85059c210", name: "Pink To Slick",
size: "S",…}
description: "Lorem Ipsum is simply dummy text of the printing and
typesetting industry. Lorem Ipsum has been the industry's standard dummy
text ever since the 1500s."
imageUrl: "http://localhost:4200/assets/PinkToSlick.png"
name: "Pink To Slick"
price: 299
size: "S"
_id: "651352899db20fb85059c210"
下面是我的架构:
var express = require('express');
const mongoose = require("mongoose");
const Products = require('../backend-search/Products.cjs');
const User = require('../../app/backend/users/user.model')
const productSchema = new mongoose.Schema({
name:{
type: String,
},
size:{type: String},
imageUrl:{ type:String},
price:{type:Number},
description:{
type:String
}
});
const cartitemSchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
product: [ productSchema],
active: {
type: Boolean,
default: true
},
modifiedOn: {
type: Date,
default: Date.now
}
},
{ timestamps: true },
);
module.exports =
mongoose.model('Cartitem', cartitemSchema, 'cartitems');
//Here is my code
router.post('/getProducts/addtocart', async(req, res)=>{
const { name, description,size, imageUrl, price} = req.body
_id = req.params._id;
//hard coded for now
userId =({"_id" : ObjectId("6588baa65a346036b4479398")});
try{
// -------Get users Cart ------
let cart = await Cartitem.findOne({userId:userId});
if(cart){ //means cart is not null
//-----If cart exist for user-------
let indexFound = cart.product.findIndex(product => {
product._id == _id
}
//----------check if product exist
if (indexFound > -1) {
/*product exists in the cart, update the quantity *** Might have to add
this*/
let productItem = cart.product[indexFound];
console.log("Product Item" + "" + productItem);
cart.product[indexFound] = productItem;
}
mongoose.connect(mongoURI, async function(err, db){
assert.ok("1" == 1 );
db.collection('cartitems').updateOne({_id:_id},{
$push:{
product:{
_id:_id,
name:name, size:size, imageUrl:imageUrl, price:price
}
}
})
cart = await cart.save();
console.log("What is cart" + "" + "" + cart );
return res.status(201).send(cart);
});
}
//cart stops here
else{
//no cart for user, create new cart
const newCartitem = new Cartitem({
userId,
product: [{
name,
size,
imageUrl,
price,
description
}]
});
const purchase = newCartitem.save()
return res.status(201).send({purchase});
}
} catch (err) {
console.log(err);
res.status(500).send("Something went wrong");
}
});
提前感谢你 PDH
最佳回答
暂无回答