0

Overview: I'm needing to update an array that contains image links with any new image links. At the same time I'm keeping all previously uploaded images in the array. My problem is that while doing this the previous image links get combined. Example below. How would I change my code to fix the array? Thanks for any help.

    var allimages = []

    var allCurrentImages = req.body.oldimages
    //this pulls all the previous image links

        if (allCurrentImages && allCurrentImages.length > 2){
         for (i=0;i<allCurrentImages.length;i++){
         allimages.push(allCurrentImages[i]);
         }
    }

    if (filepath && filepath.length > 2){
    allimages.push(filepath);
    }

PROBLEM

Here's the problem. If var allCurrentImages has two images in it the array combines them into one item because I'm requesting the body. It looks like this when there are 3 images:

images[0] =  uploads/598f4cc,uploads/53eew2w
images[1] =  uploads/7wusjw2w

It needs to look like this:

images[0] = uploads/598f4cc
images[1] = uploads/53eew2w
images[2] = uploads/7wusjw2w

So I need to somehow split the req.body.oldimages into separate parts before pushing it to the array. (I think.) Any help or advice is greatly appreciated!

5
  • 1
    That output is simply impossible? If allCurrentImages is an array, and it has two images, the condition never runs, as the array would need a length of 3 or more before the condition runs ? Commented Aug 14, 2017 at 21:54
  • @adeneo I could be wrong but I was thinking the .length was checking the number of characters in the text not objects in array. With two images the length is 48 Commented Aug 14, 2017 at 22:02
  • @adeneo Also when splitting it splits like below. Any idea why? u,p,l,o,a,d,s,/,5,9,8,f,4,c,c,0,9,8,3,e,f,7,1,1,0,a,d,2,7,e,e,6,1,5,0,2,7,4,8,3,5,9,9,9,2 Commented Aug 14, 2017 at 22:11
  • 1
    So is it a string then? The problem is that if it is a string, you wouldn't get that output either, the only way you'd get uploads/598f4cc,uploads/53eew2w as a single item when iterating, was if req.body.oldimages was in fact an array, otherwise it would be like this -> jsfiddle.net/x11dxdzj/2 Commented Aug 14, 2017 at 22:36
  • @adeneo Thanks a ton for the help. Your insight helped me figure out the issue Commented Aug 15, 2017 at 15:03

5 Answers 5

1

You can split it before:

var allCurrentImagesTmp = req.body.oldimages
var allCurrentImages = [];

for (i=0;i<allCurrentImagesTmp .length;i++){
  allCurrentImages.concat(allCurrentImagesTmp[i].split(","));
}
...
// your code
Sign up to request clarification or add additional context in comments.

Comments

1

Hum I am not really sure about your aim, I understand that some time you got string & sometime Array and you want to add element at the beginning of another array... I think a correct & proper way to do this is something simple as:

let allimages = []

let allCurrentImages = req.body.oldimages.split(',');
//Split by coma

allimages = allimages.concat(allCurrentImages);
// attention contact return the concat array so you have to set it to a variable.

This code should work but only if images does not have "," in their name, if you want to control this you will have to prevent in front-end & backend with a regex.

Comments

1

Is the req.body.oldimages an array of strings? If so, you should be able to achieve what you are looking for by changing a line to your code from this:

allimages.push(allCurrentImages[i]);

to this:

allimages.push(allCurrentImages[i].split(','));

Otherwise, since it seems that it may be one long string, you could try a more precise method of looking for the commas specifically and using that information to your advantage:

var CurrentImages = allCurrentImages; // Use temp variable to protect original
var CommaIndex = CurrentImages.indexOf(','); // Find index of first comma
while (CommaIndex>0) {  // If there is no comma present, indexOf returns -1
    allimages.push(CurrentImages.substring(0, CommaIndex-1)); // Push the first image path to allimages
    CurrentImages = CurrentImages.substring(CommaIndex+1, CurrentImages.length-1); // Grab the rest of the string after the first comma
    CommaIndex = CurrentImages.indexOf(','); // Check for another comma
}
allimages.push(CurrentImages);  // This pushes the final one after the last comma - or the only one if there was no comma.

2 Comments

For some reason it splits like this: u,p,l,o,a,d,s,/,5,9,8,f,4,c,c,0,9,8,3,e,f,7,1,1,0,a,d,2,7,e,e,6,1,5,0,2,7,4,8,3,5,9,9,9,2
@AndrewLeonardi - It looks as if req.body.oldimages might just be a string, instead of an array of strings. See my revisions above.
0

Change simply your code in this, removing also the for which loops over all the chars of the string, and it should work even for more than 2 images:

allimages.concat(allCurrentImages.split(','));

2 Comments

Looks like this should work but it always freezes the site on the second image. I've noticed the split(',') is causing it to split like this which I'm betting is a key issue: u,p,l,o,a,d,s,/,5,9,8,f,4,c,c,0,9,8,3,e,f,7,1,1,0,a,d,2,7,e,e,6,1,5,0,2,7,4,8,3,5,9,9,9,2
You need to remove the for otherwise you will loop over all the chars of your string, not all the images. Updated the code
0

The issue was the Mongoose model "oldimages" was an array but printing it outwith EJS printed it out as a string. I got around this by avoiding printing out the EJS and pulled the array from foundListings.currentimages.

Thanks for all of the help.

    Listings.findById(req.params.id, function(err, foundListings){

     var allimages = []

    var allCurrentImages = foundListings.currentimages;
    console.log('all images' + allCurrentImages)


        if (allCurrentImages){
         for (i=0;i<allCurrentImages.length;i++){
         allimages.push(allCurrentImages[i]);
         }
    }

    if (filepath && filepath.length > 2){
    allimages.push(filepath);
    }

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.