Node.js SyntaxError:意外标记'';在<;路径>;在编译ejs时
嘿,堆栈溢出的天才们Node.js SyntaxError:意外标记'';在<;路径>;在编译ejs时,node.js,ejs,multer,Node.js,Ejs,Multer,嘿,堆栈溢出的天才们 *与这个问题有关 我正在创建一个身份证生成器站点,您可以在其中输入您的详细信息以生成身份证和身份证 我使用的是node js 我正在使用ejs模板引擎 使用multer传递图像和req主体 我在听年轻的m.A.在我打字时的汽车自白 问题 我有一个服务器端验证,它应该通过将错误消息和req.body传递到ejs模板来重新呈现页面来显示错误,以显示错误并重新填充输入字段。我已经做了几天了,但不知道错误来自何处 这是错误 在这里,您可以看到我正在使用multer对其进行验证,如果
*与这个问题有关
我有一个服务器端验证,它应该通过将错误消息和req.body传递到ejs模板来重新呈现页面来显示错误,以显示错误并重新填充输入字段。我已经做了几天了,但不知道错误来自何处
这是错误
在这里,您可以看到我正在使用multer对其进行验证,如果发生错误,它将使用输入和错误再次呈现formDetails页面
这是我的ejs模板文件
<% if(error){ %>
<% var Fields = Fields.Fields %>
<% var error = error.error %>
<% } else { %>
<% var Fields = { %>
<% name : "", %>
<% regNumber : "", %>
<% department : "", %>
<% gradYear : "", %>
<% phone : "", %>
<% avatar : "" %>
<% } %>
<% var error = "its empty" %>
<% } %>
<div class="container mt-4">
<form method="POST" action="/create" class="row bg-light py-3 " autocomplete="off" enctype="multipart/form-data" >
<div class="col-12 mx-auto col-md-10 col-lg-7">
<div class="alert alert-danger" >
<%= error %>
</div>
<div class="form-floating mb-3">
<input type="text" class="form-control" name="name" value="<%= Fields.name %>" id="name" placeholder="name">
<label for="name">Your Fullname</label>
</div>
<div class="form-floating mb-3">
<input type="number" class="form-control" name="regnumber" value="<%= Fields.regNumber %>" id="floatingInput" placeholder="regidteration number">
<label for="floatingInput">Your Regnumber</label>
</div>
<div class="form-floating mb-3">
<input type="text" name="department" class="form-control" value="<%= Fields.department %>" id="floatingPassword" placeholder="Department">
<label for="floatingPassword">Department</label>
</div>
<div class="form-floating mb-3">
<input type="date" class="form-control" name="gradYear" id="floatingGrad" value="<%= Fields.gradYeear %>" placeholder="Expected year of graduation">
<label for="floatingGrad">Year of graduation</label>
</div>
<div class="form-floating mb-3">
<input type="number" class="form-control" name="phone" id="Telephone" value="<%= Fields.phone %>" placeholder="Expected year of graduation">
<label for="Telephone">Phone number</label>
</div>
<div class="mb-3">
<input class="form-control" name= "avatar" type="file" id="formFile" accept="image/jpg, image/jpeg, image/png">
</div>
</div>
<div class="col-12"></div>
<button type="submit" class="btn btn-primary col-4 col-lg-3 mx-auto"> Submit</button>
</form>
</div>
你的全名
您的注册号
部门
毕业年份
电话号码
提交
app.get("/create", function(req,res){
error = false
res.render("FormDetails", error)
})
app.post("/create",(req,res)=>{
upload(req,res, function(err){
// here multer error is gon be called on the file size
if (err instanceof multer.MulterError) {
error= "Make sure your image size is 1mb or less";Fields=req.body
return res.render("FormDetails", {error,Fields})
}
else if (err) {
error = err.message; Fields=req.body
return res.render("FormDetails", {error, Fields})
}
else if (!req.file) {
error ="an image is required"; Fields=req.body
return res.render("FormDetails",{error, Fields})
}
res.send("you have uploaded your file successfully")
})
})
<% if(error){ %>
<% var Fields = Fields.Fields %>
<% var error = error.error %>
<% } else { %>
<% var Fields = { %>
<% name : "", %>
<% regNumber : "", %>
<% department : "", %>
<% gradYear : "", %>
<% phone : "", %>
<% avatar : "" %>
<% } %>
<% var error = "its empty" %>
<% } %>
<div class="container mt-4">
<form method="POST" action="/create" class="row bg-light py-3 " autocomplete="off" enctype="multipart/form-data" >
<div class="col-12 mx-auto col-md-10 col-lg-7">
<div class="alert alert-danger" >
<%= error %>
</div>
<div class="form-floating mb-3">
<input type="text" class="form-control" name="name" value="<%= Fields.name %>" id="name" placeholder="name">
<label for="name">Your Fullname</label>
</div>
<div class="form-floating mb-3">
<input type="number" class="form-control" name="regnumber" value="<%= Fields.regNumber %>" id="floatingInput" placeholder="regidteration number">
<label for="floatingInput">Your Regnumber</label>
</div>
<div class="form-floating mb-3">
<input type="text" name="department" class="form-control" value="<%= Fields.department %>" id="floatingPassword" placeholder="Department">
<label for="floatingPassword">Department</label>
</div>
<div class="form-floating mb-3">
<input type="date" class="form-control" name="gradYear" id="floatingGrad" value="<%= Fields.gradYeear %>" placeholder="Expected year of graduation">
<label for="floatingGrad">Year of graduation</label>
</div>
<div class="form-floating mb-3">
<input type="number" class="form-control" name="phone" id="Telephone" value="<%= Fields.phone %>" placeholder="Expected year of graduation">
<label for="Telephone">Phone number</label>
</div>
<div class="mb-3">
<input class="form-control" name= "avatar" type="file" id="formFile" accept="image/jpg, image/jpeg, image/png">
</div>
</div>
<div class="col-12"></div>
<button type="submit" class="btn btn-primary col-4 col-lg-3 mx-auto"> Submit</button>
</form>
</div>