【发布时间】:2020-12-10 13:40:11
【问题描述】:
我想在 mongodb 未运行时在节点 js 中显示“不成功消息”。我的代码仅在 mongod 运行时显示成功或不成功。但是当我关闭 mongo 时,我的网页继续加载提交表单。什么都没有发生/显示。
下面是代码:
const express = require("express");
const path = require("path");
const app = express();
var favicon = require('serve-favicon')
.
.
.
mongoose.connect('mongodb://localhost/ContactForm', {useNewUrlParser: true});
port = 5500;
var ContactSchema = new mongoose.Schema({
name: String,
phone: Number,
email: String,
Company_name: String,
desc: String,
});
var Contact = mongoose.model('Contact', ContactSchema);
.
.
.
.
.
app.post("/Contact_Us",(req,res)=>{
var Myform = new Contact(req.body);
Myform.save()
.then(item => {
res.status(200).render('contact.pug',{ messageq: true});
res.end();
})
.catch(err =>{
res.status(400).send("Unable to save the message , Please Try after some time");
});
});
.
.
.
.
【问题讨论】: