1

I'm using Algolia instantsearch and I'm trying to remove public from the image url so that it can display the image. The url looks like this public/photos/436Rga0UafNBJkBe6b5X0DJL49jcife687sqqgeI.jpegon Algolia indices attributes, so the final url looks like http://127.0.0.1:8000/storage/public/photos/436Rga0UafNBJkBe6b5X0DJL49jcife687sqqgeI.jpeg. I want to remove that public so it can be http://127.0.0.1:8000/storage/photos/436Rga0UafNBJkBe6b5X0DJL49jcife687sqqgeI.jpeg. I have tried to use split('public').pop() but I get an error Cannot read property 'split' of undefined Javascript. How can I replace that?

search.addWidget(
    instantsearch.widgets.hits({
        container: '#hits',
        templates: {
            empty: 'No results',
            item: function(item) {
                return `
                <div>
                  <img src="${window.location.origin}/storage/${item.products_photos.split('public').pop()}" alt="img" class="algolia-thumb-result">
                    </div> 
                `;
            }
        }
    })
);

2 Answers 2

2

You probably need to make sure that item.products_photos exists, a simple fix would be

<img src="${window.location.origin}/storage/${(item.products_photos || '').split('public').pop()}" alt="img" class="algolia-thumb-result">
                
Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't display the image , the url looks like http://127.0.0.1:8000/storage/ @dave
then you need to look at item and see what is actually in it, right now there doesn't seem to be a .products_photos in it
2

You can use replace :

var test = "someAdress/public/someText.jpeg";
test = test.replace("public/","");
console.log(test);

So try this :

<img src="${window.location.origin}/storage/${item.products_photos.replace("public/","");}" alt="img" class="algolia-thumb-result">

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.