如何从mongoose保存功能中调用req.flash()?

时间:2017-10-30 22:49:13

标签: mongodb express mongoose mongoose-schema flash-message

在我的数据库中找到重复的用户条目后,我想使用req.flash()将消息刷新到视图。但是,User.save()函数中允许的唯一参数是错误和保存的结果。如何在那里获取请求对象,以便有 错误(即重复输入),用户会收到通知吗?

需要

>>> foo foof fofof
foo
>>> foof
>>> fofof
>>>

获取请求:

const express = require('express')
const router = express.Router()
const mongoose = require('mongoose')
const Quote = require('../models/quotes')
const User = require('../models/users')
const ObjectId = mongoose.Types.ObjectId;
const expressValidator = require('express-validator')
const flash = require('express-flash-messages')

const passport = require('passport')
const bcrypt = require('bcrypt')
const saltRounds = 10

发布请求:

/* route handling for SIGNUP page. */
router.get('/signup', (req, res, next) => {
    const flashMessages = res.locals.getMessages()
    console.log('flash:', flashMessages)
    if (flashMessages.error) {
        console.log('flash:', flashMessages)
        res.render('signup', {
            showErrors: true,
            signupErrors: flashMessages.error
        })
    } else {
        console.log('no flash errors detected.')
        res.render('signup')
    }
})

0 个答案:

没有答案