I have this function:
const getXNumberOfDocuments = async function getXNumberOfDocuments(
page,
results_per_page
) {
/*************************************************** */
results_per_page = parseInt(results_per_page);
let x_number_of_documents = await Document.find()
.populate([
{
path: "user",
populate: {
path: "profile",
select: ["profileImageURL"],
},
},
])
.limit(results_per_page)
.skip(results_per_page * page)
.lean();
/*************************************************** */
// I will stop this loop at i=0 by throwing an error
// As you se below
for (let i = 0; i < x_number_of_documents.length; i++) {
console.log("🚀 ~ file: documentsServices.js ~ line 295 ~ i", i);
// #BUG:
// For some reason this changes
// profileImageURL for i=0 and i=1 as well
x_number_of_documents[i].user.profile.profileImageURL =
"THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!";
// await s3_config.getImageReadSignedUrl(
// x_number_of_documents[i].user.profile.profileImageURL
// );
/*************************************************** */
// I check all documents here
// And I find that profileImageURL was modified in all of them
// Even though I stopped the loop at i=0
for (let j = 0; j < x_number_of_documents.length; j++) {
console.log(
"🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 j,",
j
);
console.log(
"🚀 ~ file: documentsServices.js ~ line 299 ~ x_number_of_documents[j].user.profile.profileImageURL",
x_number_of_documents[j].user.profile.profileImageURL
);
console.log(
"🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 "
);
}
/*************************************************** */
throw new Error("STOPPING LOOP AT i=0");
/*************************************************** */
}
return x_number_of_documents;
};
It usually returns something like this:
const response = [
{
user: {
profile: {
profileImageURL: "https://xxxxxxx.com/xxx.jpeg",
},
},
},
{
user: {
profile: {
profileImageURL: "https://xxxxxxx.com/yyy.jpeg",
},
},
},
{
user: {
profile: {
profileImageURL: "https://xxxxxxx.com/zzz.jpeg",
},
},
},
];
I am trying to modify profileImageURL
in each object:
for (let i = 0; i < x_number_of_documents.length; i++) {
console.log("🚀 ~ file: documentsServices.js ~ line 295 ~ i", i);
x_number_of_documents[i].user.profile.profileImageURL =
"THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!";
}
The problem is I find out that when i==0
, instead of modifying profileImageURL
ONLY the first object, it modifies it in all objects.
So when I break the loop after i==0
, and log all the objects, I see that profileImageURL
is the same in all of them:
🚀 ~ file: documentsServices.js ~ line 291 ~ x_number_of_documents.length 5
🚀 ~ file: documentsServices.js ~ line 295 ~ i 0
🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 j, 0
🚀 ~ file: documentsServices.js ~ line 299 ~ x_number_of_documents[j].user.profile.profileImageURL THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!
3asba
🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀
🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 j, 1
🚀 ~ file: documentsServices.js ~ line 299 ~ x_number_of_documents[j].user.profile.profileImageURL THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!
3asba
🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀
🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 j, 2
🚀 ~ file: documentsServices.js ~ line 299 ~ x_number_of_documents[j].user.profile.profileImageURL THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!
3asba
🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀
🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 j, 3
🚀 ~ file: documentsServices.js ~ line 299 ~ x_number_of_documents[j].user.profile.profileImageURL THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!
3asba
🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀
🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 j, 4
🚀 ~ file: documentsServices.js ~ line 299 ~ x_number_of_documents[j].user.profile.profileImageURL THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!
3asba
🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀
🚀 ~ file: documents.js ~ line 204 ~ err Error: STOPPING LOOP AT i=0
Any idea what’s going on?
To better explain this, I simplified the snippets above:
x_number_of_documents[0].user.profile.profileImageURL =
"THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!";
console.log(
"🚀 ~ file: documentsServices.js ~ line 308 ~ x_number_of_documents[0].user.profile.profileImageURL",
x_number_of_documents[0].user.profile.profileImageURL
);
console.log(
"🚀 ~ file: documentsServices.js ~ line 308 ~ x_number_of_documents[1].user.profile.profileImageURL",
x_number_of_documents[1].user.profile.profileImageURL
);
console.log(
"🚀 ~ file: documentsServices.js ~ line 308 ~ x_number_of_documents[2].user.profile.profileImageURL",
x_number_of_documents[2].user.profile.profileImageURL
);
console.log(
"🚀 ~ file: documentsServices.js ~ line 308 ~ x_number_of_documents[3].user.profile.profileImageURL",
x_number_of_documents[3].user.profile.profileImageURL
);
console.log(
"🚀 ~ file: documentsServices.js ~ line 308 ~ x_number_of_documents[4].user.profile.profileImageURL",
x_number_of_documents[4].user.profile.profileImageURL
);
So even if I explicitly, changed the first object, this is the result:
🚀 ~ file: FILLING profileImageURL for i= 0
🚀 ~ file: documentsServices.js ~ line 308 ~ x_number_of_documents[0].user.profile.profileImageURL THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!
🚀 ~ file: documentsServices.js ~ line 308 ~ x_number_of_documents[1].user.profile.profileImageURL THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!
🚀 ~ file: documentsServices.js ~ line 308 ~ x_number_ofdocuments[2].user.profile.profileImageURL THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!
🚀 ~ file: documentsServices.js ~ line 308 ~ x_number_of_documents[3].user.profile.profileImageURL THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!
🚀 ~ file: documentsServices.js ~ line 308 ~ x_number_of_documents[4].user.profile.profileImageURL THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!
So the final result is like this even though I explicitly only modified the first object!
const response = [
{
user: {
profile: {
profileImageURL:
"THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!",
},
},
},
{
user: {
profile: {
profileImageURL:
"THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!",
},
},
},
{
user: {
profile: {
profileImageURL:
"THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!",
},
},
},
];
Strangely enough, if I modify the user
instead of x_number_of_documents[0].user.profile.profileImageURL
, it modified only the first object:
const response = [
{
user: "THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!",
},
{
user: {
profile: {
profileImageURL: "https://xxxxxxx.com/yyy.jpeg",
},
},
},
{
user: {
profile: {
profileImageURL: "https://xxxxxxx.com/zzz.jpeg",
},
},
},
];
And if I modify user.profile
, I get the same original problem:
const response = [
{
user: {
profile:
"THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!",
},
},
{
user: {
profile:
"THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!",
},
},
{
user: {
profile:
"THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!",
},
},
];
So this may have something to do with modifying values in embedded documents.
2
Answers
Still no idea what's causing this weird bug, but I managed to solved it like this:
If you want to update only first object of the array, you can break the loop by adding condition
and if the requirement is to update only first object of the array you don’t need loop through the array, you can do as follows