0
votes

(node:10148) UnhandledPromiseRejectionWarning: TypeError: req.render is not a function at E:\desktop\Node.js learn project\node.jsBlog\nodejsBlogProject\routes\main.js:12:13 at processTicksAndRejections (internal/process/task_queues.js:93:5) (Use node --trace-warnings ... to show where the warning was created) (node:10148) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:10148) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

main.js ;

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

router.get('/', (req, res) => {
    res.render('site/index')
})

router.get('/blog', (req, res) => {

    Post.find({}).then(posts => {
        req.render('site/blog', {posts:Post})
    });
    res.render('site/blog');
});

router.get('/contact', (req, res) => {
    res.render('site/contact')
})

router.get('/about', (req, res) => {
    res.render('site/about')
})

router.get('/login', (req, res) => {
    res.render('site/login')
})

router.get('/register', (req, res) => {
    res.render('site/register')
})

module.exports = router

app.js;

const path = require('path')
const express = require('express')
const exphbs = require('express-handlebars')


const app = express()
const port = 3000
const hostname = '127.0.0.1'
const mongoose = require('mongoose')
const bodyParser = require('body-parser')


mongoose.connect('mongodb://127.0.0.1/nodeblog_db', {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useFindAndModify: false,
    useCreateIndex: true
});

app.use(express.static('assets'))

app.engine('handlebars', exphbs())
app.set('view engine', 'handlebars')

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

const main = require('./routes/main')
const posts = require('./routes/posts') 
app.use('/', main)
app.use('/posts', posts)

app.listen(port , hostname, () => {
    console.log(`server working now, http://${hostname}:${port}/`)
})

posts.js;

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

router.get('/new', (req, res) => {
    res.render('site/addPost')
})

router.post('/test', (req, res) => {
    console.log(req.body);
    Post.create(req.body)
    res.redirect('/')
})

module.exports = router

Post.js;

const mongoose = require('mongoose');

const PostSchema = new mongoose.Schema({
    title: { type: String, require:true},
    content: { type: String, require: true},
    date: { type: Date , default: Date.now}
})

module.exports  = mongoose.model('Post', PostSchema)

I am using mongodb as database. I get this error when I try to use each command in blog.handlebars.

1

1 Answers

0
votes

On this line:

req.render('site/blog', {posts:Post})

You wrote req when it should be res. The value that you pass to posts is also incorrect, you're passing the Post model but should use the posts you get from the callback. Finally, you potentially call res.render twice, once in the callback function and a second time afterwards. It makes more sense to call it in the case of a failure, e.g. the database call failed.

Put together it might look like this:

router.get('/blog', (req, res) => {

    Post.find({}).then(posts => {
        res.render('site/blog', { posts: posts });
    }).catch(err => {
        console.log(err);
        res.render('site/blog');
    });
});