Trying to render handlebar template in an express handlebars file (Uncaught ReferenceError: handlebars is not defined)

Trying to render handlebar template in an express handlebars file (Uncaught ReferenceError: handlebars is not defined)

问题陈述 我能够呈现来自数据库的消息。但是当我试图从 javascript 文件中呈现消息时,它说 未捕获的 ReferenceError:未定义车把

我做了什么

我有一个文件 chat.hbs(快递车把) 我渲染了文件

res.render('chat', {chatroom, chats,user})

这使我能够从数据库中获取聊天详细信息并进行渲染。 呈现数据库中的消息后,我想使用 socket.io 在聊天室中开始双方之间的聊天。

里面chat.hbs

  <div class="chat__main">
        {{!-- For messages from Database --}}
        <div class="chat__messages">
          {{#each chats}}
            <div class="message">
                <p>
                    <span class="message__name">{{sender.name}}</span>
                    <span class="message__meta">{{moment createdAt format="lll"}}</span>
                </p>
                <p> {{chat}}</p>
            </div>
         {{/each}}
        {{!-- For messages from clientside socket.io via handlebars --}}
        <div id="messages"></div> 
        </div> 
   </div>

把手模板

        <script id="message-template" type="text/x-handlebars-template">
    <div class="message">
        <p>
            <span class="message__name">\{{username}}</span>
            <span class="message__meta">\{{createdAt}}</span>
        </p>
        <p>\{{message}}</p>
    </div>
    </script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.2.0/handlebars.min.js"></script>

chat.hbs 文件中脚本标签内的客户端 JS

  <script>
       socket.on('message', (message) => {
                const html = handlebars.render(messageTemplate, {
                    username: message.userName,
                    message: message.text,
                    createdAt: moment(message.createdAt).format("Do MMM YYYY, h:mm a")
                })
                $messages.insertAdjacentHTML('beforeend',html)

            })
     </script>

应该是Handlebars.compile,不是handlebars.render[见https://handlebarsjs.com/]