node.js - 如何在我的 ejs 文件成功输出中添加 sweetalert?

标签 node.js ejs sweetalert

我有一个渲染 ejs 文件之一的 nodejs 文件,我需要在成功渲染时显示一个 ssweet 警报?目前我收到错误,显示“警报未定义” 这是我的 EJS COE

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title></title> 
<link rel='stylesheet' href='cdnjs.cloudflare.com/ajax/…'> 
<link rel="stylesheet" href="/css/style.css" type="text/css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script> 
</head> 
<body> 
<div class="verify"> 
<form id="checkForm" method="post" action="/verify"> 
<label for="verify"><b>Verify</b></label> 
<input type="text" placeholder="Register no" name="emply_id" required> 
<button type="submit" class="signupbtn">Check In</button> 
</form> 
</div> 
<!--<form id="signp" method="get" action="/signup"> 
<button type="submit" class="signupbtn" >Signup</button> 
</form>--> 

<div class="container" id="container"> 
<div class="form-container sign-up-container"> 
<form id="signupForm" method="post" action="/signup"> 
<h1>Create Account</h1> 
<div class="social-container"> 
<a href="#" class="social"><i class="fab fa-facebook-f"></i></a> 
<a href="#" class="social"><i class="fab fa-google-plus-g"></i></a> 
<a href="#" class="social"><i class="fab fa-linkedin-in"></i></a> 
</div> 
<span>or use your email for registration</span> 
<input type="text" placeholder="Company Name" name="name"required/> 
<input type="email" placeholder="Email" name="email" required/> 
<input type="password" placeholder="Password" name="psw" required/> 
<input type="password" placeholder="Re-Password" name="pswrepeat" required/> 
<input type="address" placeholder="Addresss" name="address" required/> 

<button id="myButton">Sign Up</button> 
</form> 
</div> 
<div class="form-container sign-in-container"> 
<form id="loginForm" method="post" action="/login"> 
<h1>Sign in</h1> 
<div class="social-container"> 
<a href="#" class="social"><i class="fab fa-facebook-f"></i></a> 
<a href="#" class="social"><i class="fab fa-google-plus-g"></i></a> 
<a href="#" class="social"><i class="fab fa-linkedin-in"></i></a> 
</div> 
<span>or use your account</span> 
<h1>Log in </h1> 
<hr> 
<label for="email"><b>email</b></label> 
<input type="text" placeholder="Email" name="email" required > 
<label for="password"><b>password</b></label> 
<input type="text" placeholder="Enter Password" name="password" required> 
<button type="submit" class="signupbtn">Log in</button> 
</form> 
</div> 

<div class="overlay-container"> 
<div class="overlay"> 
<div class="overlay-panel overlay-left"> 
<h1>Welcome Back!</h1> 
<p>To keep connected with us please login with your personal info</p> 
<button class="ghost" id="signIn">Sign In</button> 
</div> 
<div class="overlay-panel overlay-right"> 
<h1>Hello, Friend!</h1> 
<p>Enter your personal details and start journey with us</p> 
<button class="ghost" id="signUp">Sign Up</button> 
</div> 
</div> 
</div> 
</div> 


<script src="/js/index.js"></script> 

<script> 

$(document).ready(function () { 
if ("<%= alert %>" === "true") { 
var alertTitle = "<%= alertTitle %>" 
var alertMessage = "<%= alertMessage %>" 
swal(alertTitle, alertMessage, "error"); 
} 
}) 





</script> 


</body> 
</html>

这是我的 NODEJS 代码

app.post('/signup',urlencodedParser,async function(req,res){ 
//console.log(req) 
var name1 = req.body.name; 
var email1 = req.body.email; 
var pass = req.body.psw; 
var con_pass = req.body.pswrepeat; 
var address1 = req.body.address; 
var stat =0; 
var echeck = await emailcheck(email1) 
console.log(echeck) 
var sig ={name:name1,email:email1,password:pass,password1:con_pass,address:address1,status:stat} 
console.log(name1); 
if(con_pass == pass){ 
var cd = sha256(email1) 
//res.end("sigup submitted"); 
//res.render('login'); 

var query = db.query('INSERT INTO signup SET ?', sig, async function(err, result) { 
// Neat! 

console.log(query.sql); 
var activationinsert = await activation_insert(cd) 
console.log(activationinsert) 
var link="http://"+req.get('host')+"/emaillogin?code="+cd; 
var actmail = await activation_mail(email1,link) 
console.log(actmail) 
console.log("get me sweet"); 
res.render('login', {alert: true, alertTitle: "Oops!", alertMessage: "Something went wrong!"})  

// }); 
}); 

我用过这个方法,这个标签方法在html中工作得很好,但是当涉及到js时,就一塌糊涂了。

最佳答案

app.render 中传递参数将页面渲染为字符串时可用。这也可以在 <script> 内找到。标签。您的代码将如下所示:

<!DOCTYPE html>
<html lang="en">
  <head>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>
  </head>
  <body>
    <script type="text/javascript">
      $(document).ready(function () {
        if ("<%= alert %>" === "true") {
          var alertTitle = "<%= alertTitle %>"
          var alertMessage = "<%= alertMessage %>"
          swal(alertTitle, alertMessage, "error");
        }
      })
    </script>
  </body>
</html>

enter image description here

关于node.js - 如何在我的 ejs 文件成功输出中添加 sweetalert?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56142415/

相关文章:

node.js - 通过 Node.js 通过 Putty 连接 Azure 虚拟机?

node.js - 将 ReasonML 函数部署到 Google Cloud Functions

javascript - mp3 功能的 js 脚本出现问题

node.js - 在 EJS 中生成星星的最简洁方法

javascript - 通过ejs文件中的javascript访问JSON

javascript - 如何在同一个php文件中的php代码之后运行javascript代码?

javascript - Sweetalert 包和 jQuery submit() 无法正常工作

node.js - 使用 Node 从 stdin 读取大输入

node.js - 谷歌驱动器列出公共(public)共享文件夹中的文件

javascript - 将 SweetAlert2 与 Angular 一起使用时如何保持范围?