I am adding a new contactUs page in my project which is having a form data to store to the mongoDB collection.
(我在项目中添加了一个新的contactUs页面,该页面具有要存储到mongoDB集合的表单数据。)
But when i set all the controller and route files and run the app i am getting this error.(但是,当我设置所有控制器和路由文件并运行应用程序时,出现此错误。)
I am not getting how to solve this error.(我没有得到如何解决此错误。)
I have made sure all the files' names are fine(我确保所有文件名都正确)
My different code files are as following
(我的不同代码文件如下)
My express.js file
(我的express.js文件)
import path from 'path'
import bodyParser from 'body-parser'
import cookieParser from 'cookie-parser'
import compress from 'compression'
import cors from 'cors'
import helmet from 'helmet'
import Template from './../template'
import userRoutes from './routes/user.routes'
import authRoutes from './routes/auth.routes'
import shopRoutes from './routes/shop.routes'
import productRoutes from './routes/product.routes'
import orderRoutes from './routes/order.routes'
import contactUsRoutes from './routes/contactUs.routes'
// modules for server side rendering
import React from 'react'
import ReactDOMServer from 'react-dom/server'
import MainRouter from './../client/MainRouter'
import StaticRouter from 'react-router-dom/StaticRouter'
import { SheetsRegistry } from 'react-jss/lib/jss'
import JssProvider from 'react-jss/lib/JssProvider'
import { MuiThemeProvider, createMuiTheme, createGenerateClassName } from 'material-ui/styles'
import { blueGrey, lightGreen } from 'material-ui/colors'
//end
//comment out before building for production
import devBundle from './devBundle'
const CURRENT_WORKING_DIR = process.cwd()
const app = express()
//comment out before building for production
devBundle.compile(app)
// parse body params and attache them to req.body
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cookieParser())
app.use(compress())
// secure apps by setting various HTTP headers
app.use(helmet())
// enable CORS - Cross Origin Resource Sharing
app.use(cors())
app.use('/dist', express.static(path.join(CURRENT_WORKING_DIR, 'dist')))
// mount routes
app.use('/', userRoutes)
app.use('/', authRoutes)
app.use('/', shopRoutes)
app.use('/', productRoutes)
app.use('/', orderRoutes)
app.use('/', contactUsRoutes)
app.get('*', (req, res) => {
const sheetsRegistry = new SheetsRegistry()
const theme = createMuiTheme({
palette: {
primary: {
light: '#21d192',
main: '#1cb77f',
dark: '#1cb77f',
contrastText: '#fff',
},
secondary: {
light: '#21d192',
main: '#1cb77f',
dark: '#1cb77f',
contrastText: '#000',
},
openTitle: blueGrey['400'],
protectedTitle: lightGreen['400'],
type: 'light'
}
})
const generateClassName = createGenerateClassName()
const context = {}
const markup = ReactDOMServer.renderToString(
<StaticRouter location={req.url} context={context}>
<JssProvider registry={sheetsRegistry} generateClassName={generateClassName}>
<MuiThemeProvider theme={theme} sheetsManager={new Map()}>
<MainRouter/>
</MuiThemeProvider>
</JssProvider>
</StaticRouter>
)
if (context.url) {
return res.redirect(303, context.url)
}
const css = sheetsRegistry.toString()
res.status(200).send(Template({
markup: markup,
css: css
}))
})
// Catch unauthorised errors
app.use((err, req, res, next) => {
if (err.name === 'UnauthorizedError') {
res.status(401).json({"error" : err.name + ": " + err.message})
}
})
export default app
contactUs.routes.js file
(contactUs.routes.js文件)
import contactUsCtrl from '../controllers/contactUs.controller'
const router = express.Router()
router.route('/api/contact/')
.post(contactUsCtrl.create)
router.param('contactUsId', contactUsCtrl.contactUsByID)
export default router
contactUs.controller.js file
(contactUs.controller.js文件)
import _ from 'lodash'
import errorHandler from '../helpers/dbErrorHandler'
const create = (req, res, next) => {
const contact = new Contact(req.body)
contact.save((err, result) => {
if (err) {
return res.status(400).json({
error: errorHandler.getErrorMessage(err)
})
}
res.status(200).json({
message: "You Complained Successfully!"
})
})
}
export default create
api-contact.js file
(api-contact.js文件)
return fetch('/api/contact/', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(contact)
})
.then((response) => {
return response.json()
}).catch((err) => console.log(err))
}
export {create}
Help me out please.I am not getting what have gone wrong
(请帮帮我。我没弄错)
ask by Chaudhary Ali Sandhu translate from so