【问题标题】:Getting "SyntaxError: Unexpected token '/' in...index.ejs while compiling ejs" error after updating npm packages更新 npm 包后出现“SyntaxError: Unexpected token '/' in...index.ejs while compiling ejs”错误
【发布时间】:2021-02-21 11:46:37
【问题描述】:

我刚刚从2.7.4 to 3.1.5 更新了EJS 以及我项目中的一些其他包。这样做后,我无法访问任何网页,而是出现错误。显然所有页面都会引发相同的错误。除此之外,我的 ejs 模板中的一些代码中出现了 VS Code 错误,这在以前并不重要。我的意思是在我更新所有 npm 包并且应用程序运行良好之前,错误就已经存在。

这是端点:

router.get("/", async (req, res) => {

    try{
        const url           = `http://api.example.co/?key=${process.env.API_KEY}`;
        const fullData      = await axios.get(url);
        
        res.render("../views/index", {data: fullData});
    }catch(err) {
        res.render("../views/index", {data: null});
    }
    
});

这是错误:

这就是我在我的 ejs 模板中获取数据的方式:

<% if(data){ %> 
    Name <%= data.name %>        
<% } %>

这是我的 ejs 模板中我的脚本标签中的代码,显示红色下划线:

<script type="text/javascript" >
    <% if(data) { %>
    var map = L.map('map').setView([<%= data.latitude %>, <%= data.longitude %>], 11);
    var marker = L.marker([<%= data.latitude %>, <%= data.longitude %>]).addTo(map);

    L.tileLayer('https://api.maptiler.com/maps/streets/{z}/{x}/{y}.png?key=boomBoomBoomm', {
    attribution: '<a href="https://www.maptiler.com/copyright/" target="_blank">&copy; MapTiler</a> <a href="https://www.openstreetmap.org/copyright" target="_blank">&copy; OpenStreetMap contributors</a>'
}).addTo(map);
    <% } %>
</script>

可能是什么问题?难道我做错了什么?某些内容的更新是否与其他内容发生冲突?请帮忙!

【问题讨论】:

    标签: javascript node.js express npm ejs


    【解决方案1】:

    发现问题。显然,较新版本的 EJS 需要以与以前不同的方式包含部分页眉和页脚。

    所以改变这个:

    <% include ./partials/header %>
    

    到

    <%- include ("./partials/footer") %>
    

    修复了问题。

    【讨论】:

      猜你喜欢
      • 2020-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-01
      • 1970-01-01
      相关资源
      最近更新 更多