I'm trying to create a nested for loop to loop through a set of price records and then for every customer ID, I want to copy that price ID from the current customer to the next customer. The code itself works fine from that regard. The issue is that I wanted to use insertMany instead of insert to lower the database roundtrip time. This is when I started encountering some of the issues below.
I’m getting a weird issue that I can’t seem to pinpoint the cause of. Whenever I push to my records array. I only get records with the same customer ID of one of the customers. In the particular example below, I should be getting 3 distinct ids but that isn’t the case. I tried a minimal version of the code and it works fine but with the actual code, it’s not working for some reason. A pinpoint into the right direction is much appreciated!
PS: Do let me know if there is any clarification needed or if my question needs editing as I'm new and want to improve my question quality.
My Current Code
let records= [];
fromCustomerPricingRecords.forEach((record) => {
toCustomerIds.forEach((customer) => {
record.customerId = customer._id;
delete record._id;
delete record.createdAt;
delete record.updatedAt;
delete record.updatedBy;
delete record.ownerId;
record._id = Random.id();
record.createdAt = new Date();
record.ownerId = user._id;
records.push(record);
});
});
CustomerPricings.rawCollection().insertMany(records);
Console.log OUTPUT [ should be getting 3 different ids in the finalRecordsArray ]
I20220811-11:28:13.969(2)? customerRecords [
I20220811-11:28:13.971(2)? Document {
I20220811-11:28:13.971(2)? businessId: '3bLDoazdGqDp42BYW',
I20220811-11:28:13.972(2)? customerId: '2Nn3J8cAadbPgW6qG',
I20220811-11:28:13.972(2)? cityId: 'all',
I20220811-11:28:13.972(2)? applicableStartAt: 2022-08-04T22:00:00.000Z,
I20220811-11:28:13.972(2)? applicableEndAt: 2022-09-05T22:00:00.000Z,
I20220811-11:28:13.972(2)? prices: [ [Object] ],
I20220811-11:28:13.973(2)? _id: 'Hj2iSXnYtE2CdCzXz',
I20220811-11:28:13.973(2)? createdAt: 2022-08-11T09:28:13.968Z,
I20220811-11:28:13.973(2)? ownerId: 'SPwHFptZJwTFvZfMG'
I20220811-11:28:13.973(2)? }
I20220811-11:28:13.973(2)? ]
I20220811-11:28:13.974(2)? toCustomerIds [
I20220811-11:28:13.975(2)? {
I20220811-11:28:13.975(2)? _id: 'v6c8XjytnbdFoPqgz',
I20220811-11:28:13.975(2)? name: 'Test_CRC',
I20220811-11:28:13.975(2)? phoneNumber: ' 201551411376',
I20220811-11:28:13.975(2)? email: 'amr.shawki44 [email protected]',
I20220811-11:28:13.976(2)? address: 'CRC@CRC_ROAD',
I20220811-11:28:13.976(2)? isPrimaryMaster: true,
I20220811-11:28:13.976(2)? active: true,
I20220811-11:28:13.976(2)? customerGroupId: '4KoaSYfGz6WPR7CBJ',
I20220811-11:28:13.976(2)? nSt: 'accepted',
I20220811-11:28:13.976(2)? nid: 'ze2FCvGqDCfTG8u9X'
I20220811-11:28:13.977(2)? },
I20220811-11:28:13.977(2)? {
I20220811-11:28:13.977(2)? _id: 'RPXoQb4hzARjECZXf',
I20220811-11:28:13.977(2)? name: 'Test_Business_3',
I20220811-11:28:13.977(2)? active: true,
I20220811-11:28:13.977(2)? customerGroupId: '4KoaSYfGz6WPR7CBJ'
I20220811-11:28:13.977(2)? },
I20220811-11:28:13.978(2)? {
I20220811-11:28:13.978(2)? _id: '2Nn3J8cAadbPgW6qG',
I20220811-11:28:13.978(2)? name: 'Test_Business_1',
I20220811-11:28:13.978(2)? customerGroupId: '4KoaSYfGz6WPR7CBJ',
I20220811-11:28:13.978(2)? active: true
I20220811-11:28:13.978(2)? }
I20220811-11:28:13.979(2)? ]
I20220811-11:28:13.979(2)? finalRecordsArray [
I20220811-11:28:13.979(2)? Document {
I20220811-11:28:13.979(2)? businessId: '3bLDoazdGqDp42BYW',
I20220811-11:28:13.979(2)? customerId: '2Nn3J8cAadbPgW6qG',
I20220811-11:28:13.979(2)? cityId: 'all',
I20220811-11:28:13.980(2)? applicableStartAt: 2022-08-04T22:00:00.000Z,
I20220811-11:28:13.980(2)? applicableEndAt: 2022-09-05T22:00:00.000Z,
I20220811-11:28:13.980(2)? prices: [ [Object] ],
I20220811-11:28:13.980(2)? _id: 'Hj2iSXnYtE2CdCzXz',
I20220811-11:28:13.980(2)? createdAt: 2022-08-11T09:28:13.968Z,
I20220811-11:28:13.980(2)? ownerId: 'SPwHFptZJwTFvZfMG'
I20220811-11:28:13.981(2)? },
I20220811-11:28:13.981(2)? Document {
I20220811-11:28:13.981(2)? businessId: '3bLDoazdGqDp42BYW',
I20220811-11:28:13.981(2)? customerId: '2Nn3J8cAadbPgW6qG',
I20220811-11:28:13.981(2)? cityId: 'all',
I20220811-11:28:13.981(2)? applicableStartAt: 2022-08-04T22:00:00.000Z,
I20220811-11:28:13.982(2)? applicableEndAt: 2022-09-05T22:00:00.000Z,
I20220811-11:28:13.982(2)? prices: [ [Object] ],
I20220811-11:28:13.982(2)? _id: 'Hj2iSXnYtE2CdCzXz',
I20220811-11:28:13.982(2)? createdAt: 2022-08-11T09:28:13.968Z,
I20220811-11:28:13.982(2)? ownerId: 'SPwHFptZJwTFvZfMG'
I20220811-11:28:13.982(2)? },
I20220811-11:28:13.983(2)? Document {
I20220811-11:28:13.983(2)? businessId: '3bLDoazdGqDp42BYW',
I20220811-11:28:13.983(2)? customerId: '2Nn3J8cAadbPgW6qG',
I20220811-11:28:13.983(2)? cityId: 'all',
I20220811-11:28:13.983(2)? applicableStartAt: 2022-08-04T22:00:00.000Z,
I20220811-11:28:13.983(2)? applicableEndAt: 2022-09-05T22:00:00.000Z,
I20220811-11:28:13.984(2)? prices: [ [Object] ],
I20220811-11:28:13.984(2)? _id: 'Hj2iSXnYtE2CdCzXz',
I20220811-11:28:13.984(2)? createdAt: 2022-08-11T09:28:13.968Z,
I20220811-11:28:13.984(2)? ownerId: 'SPwHFptZJwTFvZfMG'
I20220811-11:28:13.984(2)? }
I20220811-11:28:13.984(2)? ]
Small example that works
const fromCustomerPricingRecords= [1];
const toCustomerIds= [1,2,3]
let records= [];
fromCustomerPricingRecords.forEach((record) => {
toCustomerIds.forEach((customer) => {
record= customer
records.push(record)
});
});
console.log(records)
// OUTPUT
// [ 1, 2, 3 ]
CodePudding user response:
The reason is that your final array records[] contains the same object in each index. Inside the inner loop you add a new element via push() but also mutate prev elements (use the same record object).
CodePudding user response:
Point in the right direction -
- Notice
Hj2iSXnYtE2CdCzXz
appears 4 times in your console.log output. Once in customerRecords[0]._id and once in each of the finalRecordsArray