我正在尝试创建一个基本的电子商务应用程序。为此,我创建了2个模式,一个用于用户,另一个用于项目。我也为此创建了单独的文件夹,因此可以轻松地在控制器文件中“要求”这些文件夹
itemModel:
const mongoose = require ('mongoose');
console.log("MONGOOSE INSIDE MODEL :")
console.log(mongoose);
let Schema = mongoose.Schema;
let itemSchema = new Schema({
itemId:{
type:String,
//unique:true
},
name:{
first:String,
last:String,
},
description:{
type:String,
default:''
},
colour:{
type:String,
},
category:{
type:String,
default:''
},
added:{
type:Date
}
});
console.log("------inside ecomModel");
// no export statement here, mongoose.model handles that
//mongoose.model('Blog',blogSchema);
module.exports = mongoose.model('Item',itemSchema)
userModel:
const mongoose = require('mongoose');
console.log("MONGOOSE INSIDE MODEL :")
console.log(mongoose);
const Schema = mongoose.Schema;
let userSchema = new Schema({
userId: {
type: String,
//unique:true
},
name: {
type: String
},
cart: [
{}],
});
module.exports = mongoose.model('User', userSchema);
控制器文件具有获取特定物品,特定用户的功能以及将特定物品添加到特定用户购物车的“ addtocart”功能。
let viewByItemId = (req, res) => { //for route params use req.params.(key) for body use req.body.(key)
itemModel.findOne({ 'itemId': req.params.itemId }, (err, result) => {
if (err) {
console.log(err);
res.send(err);
} else if (result == undefined || result == '' || result == null) {
console.log("No Item Found");
res.send('No Item found');
} else {
res.send(result)
}
})
}
////////////////////
let getUser = (req, res) => {/*
userModel.findOne({userId:'req.body.userid'},(err,result)=>{
if (err) {
console.log(err);
res.send(err);
} else if (result == undefined || result == '' || result == null) {
console.log("No user Found");
res.send('No user found');
} else {
res.send(result);
console.log("user with userId " +result.userId + " found")
}
})*/
//now i have used exec(), i was trying different ways
userModel.find({ "userId": "req.body.userid" }).exec((err, result) => {
if (err) {
console.log("error inside getuser" + err);
res.status(500).send(err);
} else if (!result) {
res.status(404).send();
console.log("no users found");
} else if (result) {
console.log("result of mongodb" + JSON.stringify(result));
}
})
}
and the 'addtocart' function:
let addToCartItem = (req, res) => {
let itemId = req.body.itemId;
//console.log();
userModel.find({ "userId": "req.body.userid" }).exec(function (err, result) {//new,multi
if (err) {
console.log("error inside addtocart" + err);
res.status(500).send(err);
} else if (!result) {
res.status(404).send();
console.log("no users found");
} else if (result) {
console.log("result of mongodb" + JSON.stringify(result));
let obj = {
itemId: itemId,
}
result.cart.push(obj);
}
//console.log("No item Found");
//res.send('No item found');
})
}
when trying to search for a particular item, the function works but the specific user finding function doesn't work..it returns "result of mongodb[]"
and the "addtocart" function results in :
C:\Users\IBM_ADMIN\Desktop\JS\5 Node\node_assignment_one\node_modules\mongodb\li
b\utils.js:132
throw err;
^
TypeError: Cannot read property 'push' of undefined
at C:\Users\IBM_ADMIN\Desktop\JS\5 Node\node_assignment_one\controllers\cont
roller.js:190:25
at C:\Users\IBM_ADMIN\Desktop\JS\5 Node\node_assignment_one\node_modules\mon
goose\lib\model.js:4236:16
at (anonymous function).call (C:\Users\IBM_ADMIN\Desktop\JS\5 Node\node_assi
gnment_one\node_modules\mongoose\lib\query.js:3164:7)
at cb (C:\Users\IBM_ADMIN\Desktop\JS\5 Node\node_assignment_one\node_modules
\mongoose\lib\query.js:1369:14)
at result (C:\Users\IBM_ADMIN\Desktop\JS\5 Node\node_assignment_one\node_mod
ules\mongodb\lib\utils.js:414:17)
at executeCallback (C:\Users\IBM_ADMIN\Desktop\JS\5 Node\node_assignment_one
\node_modules\mongodb\lib\utils.js:406:9)
at handleCallback (C:\Users\IBM_ADMIN\Desktop\JS\5 Node\node_assignment_one\
node_modules\mongodb\lib\utils.js:128:55)
at self.close (C:\Users\IBM_ADMIN\Desktop\JS\5 Node\node_assignment_one\node
_modules\mongodb\lib\cursor.js:904:60)
at handleCallback (C:\Users\IBM_ADMIN\Desktop\JS\5 Node\node_assignment_one\
node_modules\mongodb\lib\utils.js:128:55)
at completeClose (C:\Users\IBM_ADMIN\Desktop\JS\5 Node\node_assignment_one\n
ode_modules\mongodb\lib\cursor.js:1043:14)
at Cursor.close (C:\Users\IBM_ADMIN\Desktop\JS\5 Node\node_assignment_one\no
de_modules\mongodb\lib\cursor.js:1056:10)
at C:\Users\IBM_ADMIN\Desktop\JS\5 Node\node_assignment_one\node_modules\mon
godb\lib\cursor.js:904:21
at handleCallback (C:\Users\IBM_ADMIN\Desktop\JS\5 Node\node_assignment_one\
node_modules\mongodb-core\lib\cursor.js:199:5)
at _setCursorNotifiedImpl (C:\Users\IBM_ADMIN\Desktop\JS\5 Node\node_assignm
ent_one\node_modules\mongodb-core\lib\cursor.js:556:38)
at self._endSession (C:\Users\IBM_ADMIN\Desktop\JS\5 Node\node_assignment_on
e\node_modules\mongodb-core\lib\cursor.js:564:46)
at Cursor._endSession (C:\Users\IBM_ADMIN\Desktop\JS\5 Node\node_assignment_
one\node_modules\mongodb-core\lib\cursor.js:190:5)
What am i doing wrong?
1. i have two different models, it's working fine for one and not for other?
2. is there a better way to add carts to a particular user's cart? i have a cart array which accepts objects..i am pushing the result into cart.