Display Images on HTML using MongoDB, But it didn't show












0















I have the code backend using Node.js and Front end in HTML. I tried to get the image stored in mongo to front end. But in HTML it doesn't shows the image. But when I paste the binary data of image in img src tag it works. Help plz.



index.js



 function loadImages() {
let isbn=''
let imgSource=''
if (CURRENT_URL.includes('#')) {
isbn = CURRENT_URL.substr(CURRENT_URL.indexOf('#') + 1,
CURRENT_URL.length);
console.log(isbn);
}

axios.get(baseUrlLocal + '/book/image/'+isbn)
.then(response => {
console.log(response.data)
document.getElementById('imgSource')
.setAttribute(
'src', 'data:image/png;base64,' +
btoa(unescape(encodeURIComponent(response.data))) +"'"
);

})
.catch(err => {
console.log(err);
});

}


HTML



 <div class="card-body" id="image-src">
<img id="imgSource" src="" alt="Red dot" />
</div>









share|improve this question



























    0















    I have the code backend using Node.js and Front end in HTML. I tried to get the image stored in mongo to front end. But in HTML it doesn't shows the image. But when I paste the binary data of image in img src tag it works. Help plz.



    index.js



     function loadImages() {
    let isbn=''
    let imgSource=''
    if (CURRENT_URL.includes('#')) {
    isbn = CURRENT_URL.substr(CURRENT_URL.indexOf('#') + 1,
    CURRENT_URL.length);
    console.log(isbn);
    }

    axios.get(baseUrlLocal + '/book/image/'+isbn)
    .then(response => {
    console.log(response.data)
    document.getElementById('imgSource')
    .setAttribute(
    'src', 'data:image/png;base64,' +
    btoa(unescape(encodeURIComponent(response.data))) +"'"
    );

    })
    .catch(err => {
    console.log(err);
    });

    }


    HTML



     <div class="card-body" id="image-src">
    <img id="imgSource" src="" alt="Red dot" />
    </div>









    share|improve this question

























      0












      0








      0








      I have the code backend using Node.js and Front end in HTML. I tried to get the image stored in mongo to front end. But in HTML it doesn't shows the image. But when I paste the binary data of image in img src tag it works. Help plz.



      index.js



       function loadImages() {
      let isbn=''
      let imgSource=''
      if (CURRENT_URL.includes('#')) {
      isbn = CURRENT_URL.substr(CURRENT_URL.indexOf('#') + 1,
      CURRENT_URL.length);
      console.log(isbn);
      }

      axios.get(baseUrlLocal + '/book/image/'+isbn)
      .then(response => {
      console.log(response.data)
      document.getElementById('imgSource')
      .setAttribute(
      'src', 'data:image/png;base64,' +
      btoa(unescape(encodeURIComponent(response.data))) +"'"
      );

      })
      .catch(err => {
      console.log(err);
      });

      }


      HTML



       <div class="card-body" id="image-src">
      <img id="imgSource" src="" alt="Red dot" />
      </div>









      share|improve this question














      I have the code backend using Node.js and Front end in HTML. I tried to get the image stored in mongo to front end. But in HTML it doesn't shows the image. But when I paste the binary data of image in img src tag it works. Help plz.



      index.js



       function loadImages() {
      let isbn=''
      let imgSource=''
      if (CURRENT_URL.includes('#')) {
      isbn = CURRENT_URL.substr(CURRENT_URL.indexOf('#') + 1,
      CURRENT_URL.length);
      console.log(isbn);
      }

      axios.get(baseUrlLocal + '/book/image/'+isbn)
      .then(response => {
      console.log(response.data)
      document.getElementById('imgSource')
      .setAttribute(
      'src', 'data:image/png;base64,' +
      btoa(unescape(encodeURIComponent(response.data))) +"'"
      );

      })
      .catch(err => {
      console.log(err);
      });

      }


      HTML



       <div class="card-body" id="image-src">
      <img id="imgSource" src="" alt="Red dot" />
      </div>






      html node.js mongodb






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 18 at 17:27









      Viraj WickramasingheViraj Wickramasinghe

      34




      34
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I don't think you can do this in node.js



              document.getElementById('imgSource')
          .setAttribute(
          'src', 'data:image/png;base64,' +
          btoa(unescape(encodeURIComponent(response.data))) +"'"
          );


          document object is available in your browser mate. In node js you can't just use this like that. If you want to render the image you have processed, then you might want to look for a front end template engine
          like




          • ejs

          • pug

          • handlebars


          since ejs's syntax is a little bit annoying and can be very confusing sometimes you can use an alternative like handlebars take a look at here
          handle bars is like a template engine. If you are familiar with Laravel or ASP.NET's Razor blade or Angular's string interpolation techniques this is very much like that so



          go to your shell and install handlebars like this




          npm install --save handlebars




          since this is a little too much of a topic to discuss as an answer I'll just provide you with this link a tutorial on how to install and use handle bars. try that mate you will be able to achieve what you are looking for.. Cheers ...






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54258802%2fdisplay-images-on-html-using-mongodb-but-it-didnt-show%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I don't think you can do this in node.js



                document.getElementById('imgSource')
            .setAttribute(
            'src', 'data:image/png;base64,' +
            btoa(unescape(encodeURIComponent(response.data))) +"'"
            );


            document object is available in your browser mate. In node js you can't just use this like that. If you want to render the image you have processed, then you might want to look for a front end template engine
            like




            • ejs

            • pug

            • handlebars


            since ejs's syntax is a little bit annoying and can be very confusing sometimes you can use an alternative like handlebars take a look at here
            handle bars is like a template engine. If you are familiar with Laravel or ASP.NET's Razor blade or Angular's string interpolation techniques this is very much like that so



            go to your shell and install handlebars like this




            npm install --save handlebars




            since this is a little too much of a topic to discuss as an answer I'll just provide you with this link a tutorial on how to install and use handle bars. try that mate you will be able to achieve what you are looking for.. Cheers ...






            share|improve this answer






























              0














              I don't think you can do this in node.js



                  document.getElementById('imgSource')
              .setAttribute(
              'src', 'data:image/png;base64,' +
              btoa(unescape(encodeURIComponent(response.data))) +"'"
              );


              document object is available in your browser mate. In node js you can't just use this like that. If you want to render the image you have processed, then you might want to look for a front end template engine
              like




              • ejs

              • pug

              • handlebars


              since ejs's syntax is a little bit annoying and can be very confusing sometimes you can use an alternative like handlebars take a look at here
              handle bars is like a template engine. If you are familiar with Laravel or ASP.NET's Razor blade or Angular's string interpolation techniques this is very much like that so



              go to your shell and install handlebars like this




              npm install --save handlebars




              since this is a little too much of a topic to discuss as an answer I'll just provide you with this link a tutorial on how to install and use handle bars. try that mate you will be able to achieve what you are looking for.. Cheers ...






              share|improve this answer




























                0












                0








                0







                I don't think you can do this in node.js



                    document.getElementById('imgSource')
                .setAttribute(
                'src', 'data:image/png;base64,' +
                btoa(unescape(encodeURIComponent(response.data))) +"'"
                );


                document object is available in your browser mate. In node js you can't just use this like that. If you want to render the image you have processed, then you might want to look for a front end template engine
                like




                • ejs

                • pug

                • handlebars


                since ejs's syntax is a little bit annoying and can be very confusing sometimes you can use an alternative like handlebars take a look at here
                handle bars is like a template engine. If you are familiar with Laravel or ASP.NET's Razor blade or Angular's string interpolation techniques this is very much like that so



                go to your shell and install handlebars like this




                npm install --save handlebars




                since this is a little too much of a topic to discuss as an answer I'll just provide you with this link a tutorial on how to install and use handle bars. try that mate you will be able to achieve what you are looking for.. Cheers ...






                share|improve this answer















                I don't think you can do this in node.js



                    document.getElementById('imgSource')
                .setAttribute(
                'src', 'data:image/png;base64,' +
                btoa(unescape(encodeURIComponent(response.data))) +"'"
                );


                document object is available in your browser mate. In node js you can't just use this like that. If you want to render the image you have processed, then you might want to look for a front end template engine
                like




                • ejs

                • pug

                • handlebars


                since ejs's syntax is a little bit annoying and can be very confusing sometimes you can use an alternative like handlebars take a look at here
                handle bars is like a template engine. If you are familiar with Laravel or ASP.NET's Razor blade or Angular's string interpolation techniques this is very much like that so



                go to your shell and install handlebars like this




                npm install --save handlebars




                since this is a little too much of a topic to discuss as an answer I'll just provide you with this link a tutorial on how to install and use handle bars. try that mate you will be able to achieve what you are looking for.. Cheers ...







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 18 at 18:59

























                answered Jan 18 at 17:35









                THEWOLFTHEWOLF

                570112




                570112






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54258802%2fdisplay-images-on-html-using-mongodb-but-it-didnt-show%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Liquibase includeAll doesn't find base path

                    How to use setInterval in EJS file?

                    Petrus Granier-Deferre