'NodeJs Mongoose Value Showing as undefined
So basically what I'm trying to do is the following, I have a webpage where I create a grocery list (on the press of the button, I send a post request with a list of items) and then do some actions on the so-called grocery list
Each Item objects consists of a ItemCode, Item Price and Name:
This is Part of my grocerylist Controller
exports.creategroceryList = asyncHandler(async (req, res, next) => {
req.body.user = req.user.id;
const gl = await GroceryList.create(req.body);
fixedloop(req.body); // Create an updated grocerylist
res.status(201).json({
success: true,
data: gl,
});
});
In the Same controller Class I also have
function fixedloop(obj) {
obj.listofItems.forEach((item) => {
for (let key in item) {
if (key == "ItemCode") {
Items.findOne({ ItemCode: item[key] }).then((data) => {
f(data);
});
function f(data) {
ftempdoc = data;
console.log(" Came from f Funct " + ftempdoc);
}
Items2.findOne({ ItemCode: item[key] }).then((data) => {
f1(data);
});
function f1(data) {
stempdoc = data;
console.log(" Came from f1 Funct " + stempdoc);
}
Items.findOne({ ItemCode: item[key] }).then((data) => {
f2(data);
});
function f2(data) {
fprice = data.ItemPrice;
console.log(" Came from f2 Funct " + fprice);
}
Items2.findOne({ ItemCode: item[key] }).then((data) => {
f3(data);
});
function f3(data) {
sprice = data.ItemPrice;
console.log(" Came from f3 Funct " + sprice);
}
if (fprice < sprice) {
flist.push(ftempdoc);
} else {
slist.push(stempdoc);
}
}
}
});
console.log(flist);
console.log(slist);
data2 = {
firstlistofItems: flist,
secondlistofItems: slist,
};
console.log("end");
const gl = UGroceryList.create(data2);
}
Simply Explained, I have the global variables and what I'm trying to do is go over the list of items that I got from the create grocerylist, and check from two different mongo schemas the price of each item, I then insert into a list the cheapest prices like shown,
the problems are that first I see the end part and a list of undefined.
Solution 1:[1]
In python index starts at 0, here you have to use range(0,5) to take this into account:
my_list=[0]*5
for i in range(0,5):
my_list[i]=(i-1)*10
print(my_list)
as to fill your vector properly.
Solution 2:[2]
List indexing starts at zero and the 2nd parameter of range is a stop value (i.e. 5 is not included), so you can break down the process in your mind (or on paper):
# my_list i (i-1)*10
my_list=[0]*5 # [0, 0, 0, 0, 0]
for i in range(1,5): # 1..4
my_list[i]=(i-1)*10 # [0,00, 0, 0, 0] 1 00
# [0, 0,10, 0, 0] 2 10
# [0, 0,10,20, 0] 3 20
# [0, 0,10,20,30] 4 30
print(my_list)
If you are looking to obtain [0,10,20,30,40] you have to think in terms of zero-based indexing:
my_list = [0]*5
for i in range(5): # 0..4 range(5) is the same as range(0,5)
my_list[i] = i*10
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DvdG |
| Solution 2 |
