我的路线不会显示

时间:2018-08-28 21:32:00

标签: javascript express

我的学习/添加不会渲染。我在路径“ _id”的错误“值”添加到“ CastError:转换为ObjectId失败”错误。我只是不明白,似乎没有什么能像我期望的那样工作。我表示很新。我尝试了各种不同的方法,但是它不会渲染/ studies / add

我的学习路线`

const express = require('express')
const router = express.Router()
const Studies = require('../models/studies')

router.get('/', (req, res) => {
  Studies.find({}, (err, studies) => {
    studies.sort(function (a, b) {
      return new Date(b.endDate) - new Date(a.endDate)
    })
    if (err) {
      console.log(err)
    } else {
      res.render('studies', {
        studies
      })
    }
  })
})

router.get('/:id', function (req, res) {
  Studies.findById(req.params.id, function (err, studies) {
    if (err) {
      console.log(err)
    } else {
      res.render('course', {
        studies
      })
    }
  })
})
router.get('/add', function (err, req, res) {
  if (err) {
    console.log(err)
  }
  res.render('addstudy')
})

module.exports = router

`

我的app.js文件

    const express = require('express')
const bodyParser = require('body-parser')
const path = require('path')
const config = require('./config/database')
const mongoose = require('mongoose')
const Studies = require('./models/studies')
const session = require('express-session')
const passport = require('passport')
// const flash = require('connect-flash')

const app = express()
mongoose.connect(config.database)
let db = mongoose.connection

// Check connection
db.once('open', function () {
  console.log('Connected to MongoDB')
})

// Check for DB errors
db.on('error', function (err) {
  console.log(err)
})

const logger = function (req, res, next) {
  console.log('loogging..')
  next()
}

app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
// Body Parser Middleware
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
app.use(logger)
// Passport Config
require('./config/passport')(passport)
// Passport Middleware
app.use(passport.initialize())
app.use(passport.session())
// Express Session Middleware
app.use(session({
  secret: 'keyboard cat',
  resave: true,
  saveUninitialized: true
}))

// Express Messages Middleware
/* app.use(require('connect-flash')())
app.use(function (req, res, next) {
  res.locals.messages = require('express-messages')(req, res)
  next()
}) */
app.use(express.static('public'))
app.get('*', function (req, res, next) {
  res.locals.user = req.user || null
  next()
})

app.get('/', (req, res) => {
  Studies.find({}, (err, studies) => {
    console.log(studies)
    if (err) {
      console.log(err)
    } else {
      res.render('index', {
        name: studies[0].name,
        description: studies[0].description
      })
    }
  })
})

let studies = require('./routes/studies')
let work = require('./routes/work')
let about = require('./routes/about')
let users = require('./routes/users')
app.use('/studies', studies)
app.use('/work', work)
app.use('/about', about)
app.use('/users', users)

app.listen(3002, () => {
  console.log('started on 3002')
})

0 个答案:

没有答案