简体   繁体   English

更新 npm 包后出现“SyntaxError: Unexpected token '/' in...index.ejs while compiling ejs”错误

[英]Getting "SyntaxError: Unexpected token '/' in...index.ejs while compiling ejs" error after updating npm packages

I just updated EJS from 2.7.4 to 3.1.5 and some other packages in my project.我刚刚将EJS2.7.4 to 3.1.5更新2.7.4 to 3.1.5以及我项目中的其他一些包。 Upon doing it, I can't access any of the webpages but getting an error instead.执行此操作后,我无法访问任何网页,而是收到错误消息。 Apparently all of the pages throws the same error.显然所有页面都抛出相同的错误。 Besides that I get VS Code errors in some of the code in my ejs template which didn't matter before.除此之外,我在我的 ejs 模板中的一些代码中遇到了 VS Code 错误,这在以前并不重要。 I mean the errors were there before I updated all the npm packages and the app worked perfectly fine.我的意思是在我更新所有 npm 包之前就存在错误,并且该应用程序运行良好。

This is the endpoint:这是端点:

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});
    }
    
});

This is the error:这是错误: 在此处输入图片说明

This is how I get data in my ejs template:这就是我在 ejs 模板中获取数据的方式:

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

This is the code in my script tags in my ejs template which shows red underline:这是我的 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>

What could be the issue?可能是什么问题? Am I doing something wrong?难道我做错了什么? Is the update of something clashing with something else?某些东西的更新是否与其他东西发生冲突? Please help!请帮忙!

Found the problem.发现问题了。 Apparently the newer versions of EJS requires including partials of header and footer in a different way than before.显然,较新版本的 EJS 需要以与以前不同的方式包含页眉和页脚的部分内容。

So changing this:所以改变这个:

<% include ./partials/header %>

to

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

fixed the issue.解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM