Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simon Dingley 1470 posts 3427 karma points c-trib
    Mar 12, 2019 @ 11:29
    Simon Dingley
    0

    Suppressing mediaResource.getById exception when requesting an item that has been deleted

    I have a custom listview that renders a list of news articles along with the image associated with the article. An error has been reported and it turns out it is due to the fact that someone has deleted the media item associated with one of the articles. I can handle the missing image in the display by showing a placeholder but what I can't seem to do is stop the underlying exception bubbling up to the user.

    The errors seems to occur when calling mediaResource.getById() in the following code:

    if (imgProp.value.length) {
        mediaResource.getById(imgProp.value).then(function (media) {
            item.image = mediaHelper.resolveFile(media, false) + "?width=400&height=240&mode=crop";
        },
            function (e) {
                console.error(e);
            });
    }
    

    I have tried wrapping it in a try/catch block but the error is still very visible to the user. Is there anything I can do to prevent the user seeing the error?

    Thanks, Simon

  • Tarik | WPPlumber 179 posts 801 karma points c-trib
    Jun 04, 2019 @ 07:59
    Tarik | WPPlumber
    0

    I suggest to get the available media items first by using the code below and then call the mediaResource.getById() when exist.

    entityResource.getAll("Media")
    .then(function(entities) {
    console.log(entities);
    }
    
  • Simon Dingley 1470 posts 3427 karma points c-trib
    Jun 20, 2019 @ 08:10
    Simon Dingley
    0

    Thanks for the reply Tarik but I don't feel that this is going to be a very scalable solution as the media library could become significant in size over time.

  • Tarik | WPPlumber 179 posts 801 karma points c-trib
    Jun 20, 2019 @ 09:20
    Tarik | WPPlumber
    0

    Then, I suggest to do a search in Media to restrict the source of articles' images.

    entityResource.search("articles", "Media")
       .then(function(mediaArray) {
           var myDoc = mediaArray; 
           alert('they are here!');
       });
    
Please Sign in or register to post replies

Write your reply to:

Draft