Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当我保存到数据库时,我不会';我没有收到我的数据_Javascript_Node.js_Database_Mongodb - Fatal编程技术网

Javascript 当我保存到数据库时,我不会';我没有收到我的数据

Javascript 当我保存到数据库时,我不会';我没有收到我的数据,javascript,node.js,database,mongodb,Javascript,Node.js,Database,Mongodb,当我在表单中输入数据以获取Smtp时,根据我的代码,我将获取数据并将其保存到我的数据库中,但它不起作用,我在mongodb中没有收到任何数据信息,我只获取日期 我的观点(ejs) 当我在表单中输入数据以获取Smtp时,根据我的代码,我将获取数据并将其保存到我的数据库中,但它不起作用,我在mongodb中没有收到任何数据信息,我只获取日期 我的路线 router.get('/mail', forwardAuthenticated, (req, res) => re

当我在表单中输入数据以获取Smtp时,根据我的代码,我将获取数据并将其保存到我的数据库中,但它不起作用,我在mongodb中没有收到任何数据信息,我只获取日期

我的观点(ejs)

当我在表单中输入数据以获取Smtp时,根据我的代码,我将获取数据并将其保存到我的数据库中,但它不起作用,我在mongodb中没有收到任何数据信息,我只获取日期

我的路线


    router.get('/mail', forwardAuthenticated, (req, res) =>
        res.render('mail', {
            user: req.user,
            mail: req.mail

        })
    );
    router.post('/mail', (req, res) => {
        const { to, cc, bcc, subject, message, attachment } = req.body;
        const { smtpUrl, smtpUsername, smtpPassword } = req.body;

        console.log(smtpUrl)
        console.log(smtpPassword)
        console.log(smtpUsername)



        let errors = [];

        if (!smtpUrl || !smtpUsername || !smtpPassword) {
            errors.push({ msg: 'Add an account' });
            res.render('mail', {
            smtpUrl,
            smtpPassword,
            smtpUsername
            });

        }else{
          console.log(smtpUrl)
          console.log(smtpPassword)
          console.log(smtpUsername)
          const newSmtp = new Smtp({
            smtpUrl,
            smtpPassword,
            smtpUsername
        });

        newSmtp
        .save()
        .then(mail => {
            req.flash(
                'success_msg',
                'Account Added'
            );

        })
        .catch(err => console.log(err));
        }
        if (!to || !subject || !message) {
            errors.push({ msg: 'Please enter all fields' });
        }


        if (errors.length > 0) {
            res.render('mail', {
                errors,
                to,
                cc,
                bcc,
                subject,
                message,
                attachment,
            });
        } else {
            const newMail = new Mail({
                to,
                cc,
                bcc,
                subject,
                message,
                attachment,
            });


            let transporter = nodemailer.createTransport({
                service: smtpUrl,
                auth: {
                    user: smtpUsername,
                    pass: smtpPassword
                }
            });

            let mailOptions = {
                from: smtpUsername,
                to: to,
                subject: subject,
                text: `${message}`
            };

            transporter.sendMail(mailOptions, function (error, info) {
                if (error) {
                    console.log(error);
                } else {

                    newMail
                        .save()
                        .then(mail => {
                            req.flash(
                                'success_msg',
                                'mail sent'
                            );

                        })
                        .catch(err => console.log(err));
                    console.log('Email sent: ' + info.response);
                }
            });





        }


    })


我没有详细介绍,但是我可以看到您没有导出Smtp,就像您对邮件所做的那样,否则您的实例化将无法工作;so module.exports=Smtp
const{to,cc,bcc,subject,message,attachment}=req.body;const{smtpUrl,smtpUsername,smtpPassword}=req.bodyx=req.body。如果您的代码正在重写第一行的内容,请按照顺序将它们放在一行中。
{x,y}=req.body
如果您的代码正在重写第一行的内容
const{to,cc,bcc,subject,message,attachment,smtpUrl,smtpUsername,smtpPassword}=req.body尝试此操作并按顺序放置。@codabae是否有任何更新?我没有详细说明,但是我可以看到您没有像导出邮件一样导出Smtp,否则您的实例化将无法工作;so module.exports=Smtp
const{to,cc,bcc,subject,message,attachment}=req.body;const{smtpUrl,smtpUsername,smtpPassword}=req.bodyx=req.body。如果您的代码正在重写第一行的内容,请按照顺序将它们放在一行中。
{x,y}=req.body
如果您的代码正在重写第一行的内容
const{to,cc,bcc,subject,message,attachment,smtpUrl,smtpUsername,smtpPassword}=req.body尝试此操作,并按照顺序放置。@codabae有更新吗?

    const mongoose = require('mongoose');

    const MailSchema = new mongoose.Schema({
        to: {
            type: String,

        },
        cc: {
            type: String,

        },
        bcc: {
            type: String,
        },
        subject: {
            type: String,

        },
        message: {  
            type: String,

        },
        attachment: {
            type: String,
        },
        date: {
            type: Date,
            default: Date.now
        },

    });

    const SmtpSchema = new mongoose.Schema({
        smtpUrl: {
            type: String,
            required: true

        },
        smtpUsername: {
            type: String,
            required: true

        },
        smtpPassword: {
            type: String,
            required: true

        },
        date: {
            type: Date,
            default: Date.now
        },

    });





    const Mail = mongoose.model('Mail', MailSchema);
    const Smtp = mongoose.model('Smtp', SmtpSchema);


    module.exports = Mail;


    router.get('/mail', forwardAuthenticated, (req, res) =>
        res.render('mail', {
            user: req.user,
            mail: req.mail

        })
    );
    router.post('/mail', (req, res) => {
        const { to, cc, bcc, subject, message, attachment } = req.body;
        const { smtpUrl, smtpUsername, smtpPassword } = req.body;

        console.log(smtpUrl)
        console.log(smtpPassword)
        console.log(smtpUsername)



        let errors = [];

        if (!smtpUrl || !smtpUsername || !smtpPassword) {
            errors.push({ msg: 'Add an account' });
            res.render('mail', {
            smtpUrl,
            smtpPassword,
            smtpUsername
            });

        }else{
          console.log(smtpUrl)
          console.log(smtpPassword)
          console.log(smtpUsername)
          const newSmtp = new Smtp({
            smtpUrl,
            smtpPassword,
            smtpUsername
        });

        newSmtp
        .save()
        .then(mail => {
            req.flash(
                'success_msg',
                'Account Added'
            );

        })
        .catch(err => console.log(err));
        }
        if (!to || !subject || !message) {
            errors.push({ msg: 'Please enter all fields' });
        }


        if (errors.length > 0) {
            res.render('mail', {
                errors,
                to,
                cc,
                bcc,
                subject,
                message,
                attachment,
            });
        } else {
            const newMail = new Mail({
                to,
                cc,
                bcc,
                subject,
                message,
                attachment,
            });


            let transporter = nodemailer.createTransport({
                service: smtpUrl,
                auth: {
                    user: smtpUsername,
                    pass: smtpPassword
                }
            });

            let mailOptions = {
                from: smtpUsername,
                to: to,
                subject: subject,
                text: `${message}`
            };

            transporter.sendMail(mailOptions, function (error, info) {
                if (error) {
                    console.log(error);
                } else {

                    newMail
                        .save()
                        .then(mail => {
                            req.flash(
                                'success_msg',
                                'mail sent'
                            );

                        })
                        .catch(err => console.log(err));
                    console.log('Email sent: ' + info.response);
                }
            });





        }


    })