Khan Saab
Khan Saab

Reputation: 511

Mustache is not defined error in node js : Uncaught error

I want to render the pages on the client side with Mustache.js but it says Mustache is not defined. I found an answer here but it's difficult to understand as i just started learning about websites/rendering etc. In order to learn i need to fix this issue but i have no clue how can i do that.

Can someone tell me what do i need to do to fix it without going into much details as i am a beginner in scripting languages.

Here is my code: I am trying to open a new page and then simple render it.

socket.on('sign-up-succes',(data) => {
  window.open(data.redirect,"_self");
  var template = jQuery('#message-template').html();
  var html = Mustache.render(template,{
    text : "Hello"
   });
  jQuery('#messages').append(html);
});

Upvotes: 3

Views: 5109

Answers (2)

front_end_dev
front_end_dev

Reputation: 2056

jsFiddle demo - http://jsfiddle.net/cvqLkb4t/

HTML

<script src="https://raw.githubusercontent.com/janl/mustache.js/master/mustache.min.js" type="application/javascript"></script>
    <script>
    socket.on('sign-up-succes',(data) => {
      window.open(data.redirect,"_self");
      var template = jQuery('#message-template').html();
      var html = Mustache.render(template,{
        text : "Hello"
       });
      jQuery('#messages').append(html);
    });
    </script>

Upvotes: 1

Vishal-L
Vishal-L

Reputation: 1347

From the code you posted I see you are using window object, so I believe you are trying to use Mustache in the client side script.

If so, then use below CDN:

https://cdnjs.cloudflare.com/ajax/libs/mustache.js/3.0.0/mustache.js

or

https://cdnjs.cloudflare.com/ajax/libs/mustache.js/3.0.0/mustache.min.js

I suggest you render templates from Server side(Node.js in your case).

Upvotes: 5

Related Questions