编译ejs时出现意外的标记“;”。

3

我目前正在使用sqlite开发Web应用程序。在这里,我想根据数据库中注册的数据类型显示多个数据。因此,我想使用case语句。但是,在调用它时,在我的视图中会出现错误。

另外,我尝试了不使用case语句来显示它,它可以完美地显示。

SyntaxError: Unexpected token ';' in C:\Users\tu\Documents\SVAV3.1\views\show_config.ejs while compiling ejs

If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
Or, if you meant to create an async function, pass async: true as an option.
    at new Function (<anonymous>)
    at Template.compile (C:\Users\tung\Documents\SmartVacAppV3.1\node_modules\ejs\lib\ejs.js:633:12)
    at Object.compile (C:\Users\tung\Documents\SmartVacAppV3.1\node_modules\ejs\lib\ejs.js:392:16)
    at handleCache (C:\Users\tung\Documents\SmartVacAppV3.1\node_modules\ejs\lib\ejs.js:215:18)
    at tryHandleCache (C:\Users\tung\Documents\SmartVacAppV3.1\node_modules\ejs\lib\ejs.js:254:16)
    at View.exports.renderFile [as engine] (C:\Users\tung\Documents\SmartVacAppV3.1\node_modules\ejs\lib\ejs.js:485:10)
    at View.render (C:\Users\tung\Documents\SmartVacAppV3.1\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\tung\Documents\SmartVacAppV3.1\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\Users\tung\Documents\SmartVacAppV3.1\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (C:\Users\tung\Documents\SmartVacAppV3.1\node_modules\express\lib\response.js:1008:7)

这是我的视图代码:

<%- include("layouts/_header") %>
<div class = "container" style="padding-top: 50px">

<div class="row">
    <form class="col-12" action="/create_config" method="POST">

        <% categories.forEach((category) => {%>
        <div class="card mb-4">
            <div class="card-header"> <%= category.name_var_category %> </div>
            <% data.forEach((values) => {%>
                <% if (values.id_variable_category === category.id) {%>  
                    <% switch(values.name_variable_types) {%>
                        <% case 'Hexadecimal': %>
                            <div class="form-group col-md-4">
                                <label for="<%= values.name_variable %>"><%= values.name_variable %></label>
                                <input type="text" class="form-control" name="<%=  values.name_variable %>" value="<%= values.value_variable %>">
                            </div>
                            <% break; %>
            
                        <% case 'Boolean': %>
                            <div class="form-group col-md-4">
                                <label for="<%=  values.name_variable %>"><%= values.name_variable %></label>
                                <select class="form-control" name="<%=  values.name_variable %>" id="<%=  values.name_variable %>">
                                    <option value="<%= values.value_variable %>"><%= values.value_variable %></option>
                                    <% if(values.value_variable === "ON") {%>
                                        <option value="<%= 'OFF' %>">OFF</option>
                                    <% }else {%>
                                        <option value="<%= 'ON' %>">ON</option>
                                    <% } %>
                                </select>
                            </div>
                            <% break; %>
            
                        <% case 'Integer': %>
                            <div class="form-group col-md-4">
                                <label for="<%=  values.name_variable %>"><%= values.name_variable %></label>
                                <input type="number" class="form-control"  name="<%=  values.name_variable %>" value="<%= values.value_variable %>" min="<%= values.start_range_variable %>" max="<%= values.end_range_variable %>" step="<%= values.step_variable %>" >
                            </div>
                            <% break; %>
            
                        <% case 'Float': %>
                            <div class="form-group col-md-4">
                                <label for="<%=  values.name_variable %>"><%= values.name_variable %></label>
                                <input type="number" class="form-control"  name="<%=  values.name_variable %>" value="<%= values.value_variable %>" min="<%= values.start_range_variable %>" max="<%= values.end_range_variable %>" step="<%= values.step_variable %>" >
                            </div>
                            <% break; %>
            
                        <% default: %>
                        <div class="alert alert-success">
                            <p>A value had no variable type</p>
                        </div>
                    <% } %>
                <% } %>
            <% }) %>
        </div>   
        <% }) %>

        <div class="form-group row">
            <div class="col-sm-10 pt-3">
                <button type="submit" class="btn btn-primary">Géréner Fichier Config</button>
            </div>
        </div>
    </form>
</div>
</div>
<%- include("layouts/_footer") %>
1个回答

3
问题在于你如何使用 switch case - 似乎ejs期望cases保留在switch-ejs表达式中。尝试像这样做:

  <% switch(values.name_variable_types) {
        case 'Hexadecimal': %>
            <div class="form-group col-md-4">
              <label for="<%= values.name_variable %>"></label>
              <input type="text" class="form-control" name="<%=  values.name_variable %>" value="<%= values.value_variable %>">
            </div>
        <% break;
        case 'Boolean': %>
        ...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接