【问题标题】:want to show Unsuccess Message for not running mongodb想要显示未运行 mongodb 的失败消息
【发布时间】: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");
    });
});

.
.
.
.

【问题讨论】:

    标签: node.js mongodb pug


    【解决方案1】:
    mongoose.connect('mongodb://localhost/ContactForm', { useNewUrlParser: true }).
    catch(error => console.log(error.message));
    

    或者自定义错误信息:

    mongoose.connect('mongodb://localhost/ContactForm', { useNewUrlParser: true }).
    catch(error => console.log("unable to connect to server));
    

    谢谢

    【讨论】:

    • 感谢先生的回复,但我想显示用户消息,例如:-“对不起,在那个contact.pug页面上出现了错误消息......详细信息:-当mongo运行时.and当用户提交他们的表单时,页面在该页面上呈现并显示消息成功,当它成功运行时使用'messageq ===true'处理。我正在使用If语句在contact.pug中处理它。但是当我不是运行然后我想在用户提交表单时向用户显示“该页面出现问题或某事”的消息。
    猜你喜欢
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 2017-08-19
    • 2017-08-12
    • 2016-12-05
    • 2011-07-02
    相关资源
    最近更新 更多