Problems sending a object key to another object (Node) - javascript

I am trying to send variable Quantity to another object that shares the same ID for Product.
(I am using MongoDB)
It even logs the result specified below but then an error happens
for (let i = 0; i < product.length; i++){
for (let j = 0; j < cartRegistry.length; j++){
if ( cartRegistry[i].ProductId === (product[j]._id.toString()) ){
console.log(product[j]._id.toString())
console.log(cartRegistry[i].ProductId, product[j]._id)
console.log('true')
}
/* console.log(quantity[i].id === product[j]._id.toString()) */
}
}
C:\Users\Toni\Documents\Aulas Node\store\routes\cart.js:22
if ( cartRegistry[i].ProductId === (product[j]._id.toString()) ){
^
TypeError: Cannot read properties of undefined (reading '_id')
at C:\Users\Toni\Documents\Aulas Node\store\routes\cart.js:22:60

Related

TypeError: Cannot read properties of undefined (reading 'length'). Codewars task

I write solutions to tasks and got
TypeError: Cannot read properties of undefined (reading 'length').
This is my solution
function sumArray(array) {
if (array === null || array.length < 2) {
return 0;
}
let sum = 0;
for (let i = 0; i < array.length; i++) {
sum += array[i];
}
return sum - Math.max(...array) - Math.min(...array)
}
Could you help me find an error in my code?
undefined is not === with null. You need to test for both before calling length on your argument, or don't call this method with an undefined argument.
You need to check is array even defined and that it is type array.
And I think you need to return 0 in all variant where argument array is not defined or null
if (!array || (Array.isArray(array) && array.length < 2)) {
return 0;
}
Based on the code you provided, it's possible that the error is being caused by calling the sumArray function with an undefined or null value as its argument. The code below is fixed.
function sumArray(array) {
if (!Array.isArray(array) || array.length < 2) {
return 0;
}
let sum = 0;
for (let i = 0; i < array.length; i++) {
sum += array[i];
}
return sum - Math.max(...array) - Math.min(...array);
}

Cannot read properties of undefined (reading 'geometry') - but hardcoding works?

I am coding a three.js simulation/animation and am having a hard time understanding why hardcoding an array index to a custom class instantiation ("physicsObjects[4]") works, but using the i variable in a for loop ("physicsObjects[i]") does not.
When successful, the updateTail() adjusts the a three.js line position attribute, and then I must set needsUpdate = true as shown in the Three.js documentation to redraw.
In the first example, it works successfully (although only for ONE of the objects, 4).
physicsObjects[i] has a .tail property which is a THREE.LineSegments(geometry,material)
for (let i = 0; i < physicsObjects.length; i++) {
physicsObjects[4].updateTail()
physicsObjects[4].tail.geometry.attributes.position.needsUpdate = true
}
It also works for 2 of my physics objects if I do this.
for (let i = 0; i < physicsObjects.length; i++) {
physicsObjects[4].updateTail()
physicsObjects[4].tail.geometry.attributes.position.needsUpdate = true
physicsObjects[7].updateTail()
physicsObjects[7].tail.geometry.attributes.position.needsUpdate = true
}
But if I want to iterate, it does NOT work.
The only difference is that I am indexing with 'i' instead of a hardcoded index?
for (let i = 0; i < physicsObjects.length; i++) {
physicsObjects[i].updateTail()
physicsObjects[i].tail.geometry.attributes.position.needsUpdate = true
}
This is the error I get.
Uncaught TypeError: Cannot read properties of undefined (reading 'geometry')
at animate (app.js:159:32)
Here's my entire animate() draw loop for reference:
function animate() {
//Frame Start up
requestAnimationFrame(animate);
//Force Application
if (frameIndex % 1 == 0) {
for (let i = 0; i < physicsObjects.length; i++) {
for (let j = 0; j < physicsObjects.length; j++) {
if (i !== j) {
let f = physicsObjects[i].attract(physicsObjects[j])
physicsObjects[i].applyForce(f)
physicsObjects[i].updatePhysics()
physicsObjects[i].updateGeometry()
}
}
}
}
//Testing adding a tail to the objects
for (let i = 0; i < physicsObjects.length; i++) {
physicsObjects[4].updateTail()
physicsObjects[4].tail.geometry.attributes.position.needsUpdate = true
}
const time = performance.now();
controls.update(time, prevTime)
renderer.render(scene, camera);
stats.update()
//Frame Shut Down
prevTime = time;
}
Why can't I iterate with a loop?

indexOf Array returning -1 even when the value is present in the array

I have a placeCollection collection which has an array called locFrnd.
As I was not able to use indexOf function on the locFrnd array of object I moved it to a new array called dup. As the main purpose of the dup was that i could use the data and get the indexOf function performed on it. $scope.Friend is the incoming data source and is also an array . user can send multiple values in $scope.Friend. The main logic that I want to check here is that if 2 values are there as user input in $scope.Friend both the values need to be checked one by one in locFrnd array.If they are not present than they need to be pushed in locFrnd array. The only challenge is that indexOf operation is referring to the last value of dup. e.g dup has r,j and $scope.Friend has r,j j from dup is compared to r from $scope.Friend and the next value is also not getting checked. I am not sure why this anonymous behavior is occurring in case of indexOf function
//if country exist
else if (cnt_exist == 1) {
alert("country exist");
var len = $scope.placeCollection[cnt_i].locFrnd.length;
for (var j = 0; j < len; j++) {
var dup = [];
dup[j] = $scope.placeCollection[cnt_i].locFrnd[j].name;
}
//check for friend now
alert("checking for friend");
//some code has to inserted here to handle Friends as it is an array
alert($scope.Friend.length);
for (var k = 0; k < $scope.Friend.length; k++) {
var frnd_exist = 0;
alert($scope.Friend[k]);
alert(dup.indexOf($scope.Friend[k]));
if (dup.indexOf($scope.Friend[k]) != -1) // friend exist
{
alert("entered friend comparison");
frnd_exist = 1;
}
if (frnd_exist == 1) // if friend does not exist
{
alert("friend exist");
} else if (frnd_exist == 0) {
var eachFriend = {
name: $scope.Friend[k]
}
$scope.placeCollection[cnt_i].locFrnd.push(eachFriend);
}
}
Answer is simple.
you are initializing dup in every iteration of the for loop. Initialize outside the loop
var dup = [];
for (var j = 0; j < len; j++) {
dup[j] = $scope.placeCollection[cnt_i].locFrnd[j].name;
}

the property of the "Stub Object" object does not meet the checkpoint's condition

"the property of the "Stub Object" object does not meet the checkpoint's condition" –I got this error, I tried every type of checkpoint.
for (i = 0; i < rows; i++) {
j = 1;
cellvalue = grid.wValue(i, j);
grid.ClickCell(i, j);
//The problem is this checkpoint;
aqObject.CheckProperty(Aliases.browser.page1921681611258080.panelTabpanelBody.p‌anelGamblerpanel.panelTabpanel.panelForm.panelFormtargetel.panelContainer2.tableD‌​ isplayfield4.panelDisplayfieldInputel, "contentText", cmpEqual, "995");
}
The error you get means that the object you pass to the CheckProperty method is not found. You can check the object for existence before calling the checkpoint.
for (i = 0; i < rows; i++) {
j = 1;
cellvalue = grid.wValue(i, j);
grid.ClickCell(i, j);
var obj = Aliases.browser.page1921681611258080.panelTabpanelBody.p‌anelGamblerpanel.panelTabpanel.panelForm.panelFormtargetel.panelContainer2.tableD‌​ isplayfield4.panelDisplayfieldInputel;
//The problem is this checkpoint;
if (false == obj.Exists)
Log.Error("The object is not found");
else
aqObject.CheckProperty(, "contentText", cmpEqual, "995");
}

JavaScript proper way to handle null in JSON object

Here is the code :
for (i = 0; i < data.RecruitingGroups.length; i++) {
data.RecruitingGroups[i].id = i;
if (data.RecruitingGroups[i].Rule.Rules != null) {
for (j = 0; j < data.RecruitingGroups[i].Rule.Rules.length; i++) {
data.RecruitingGroups[i].Rule.Rules[j].id = j;
}
}
}
Problem is that sometimes RecruitingGroups[].Rule is null. So I tried to verify it was not null before continuing and running the next for loop, but it still throws the error:
Uncaught TypeError: Cannot read property 'Rules' of null
How can i bypass this error.
your second loop needs to increment j++ not i++. =)
You're testing if Rule.Rules is null. That's not the problem; Rule itself is null, as evidenced by the error message. You need to test both Rule and Rule.Rules if either can be null.
Try
if (data.RecruitingGroups[i].Rule && data.RecruitingGroups[i].Rule.Rules) {
if (data.RecruitingGroups[i].Rule && data.RecruitingGroups[i].Rule.Rules) {
for (j = 0; j < data.RecruitingGroups[i].Rule.Rules.length; i++) {
data.RecruitingGroups[i].Rule.Rules[j].id = j;
}
}

Categories