无法读取null的属性“图像”

时间:2019-07-09 14:45:43

标签: javascript node.js node-modules

在module.exports处无法读取null的属性“图像”

module.exports = (req, res, next) => {
if (!req.files.image) {
    return res.redirect('/posts/new')
}

next()}

index.js:

const express = require('express')

const app = new express()

const path = require('path')

const expressEdge = require('express-edge')

const mongoose = require('mongoose')

const bodyParser = require('body-parser')

const Post = require('./database/models/Post')

const fileUpload = require('express-fileupload')



mongoose.connect('mongodb://localhost/node-js-blog', { useNewUrlParser:       true })

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

app.use(fileUpload())

app.use(expressEdge)

app.set('views', `${__dirname}/views`)

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

const storePost = require('./middleware/storePosts')
app.use('/posts/store', storePost)

app.get('/',async (req, res)=>{
const posts = await Post.find({})
console.log(posts)
res.render('index',{
    posts
})
})

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

app.get('/post/:id', async (req,res)=>{
const post = await Post.findById(req.params.id)
res.render('post',{
    post
})
})

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

app.get('/posts/new', (req,res)=>{
res.render('create')
})

app.post("/posts/store", (req, res) => {
const {image} = req.files

image.mv(path.resolve(__dirname, 'public/posts', image.name), (error) => {
    Post.create({
        ...req.body,
        image: `/posts/${image.name}`
    }, (error, post) => {
        res.redirect('/');
    });
})
});

app.listen(3000, ()=>{
console.log('start server')
})

create.edge:

<div class="form-group mt-5">
                    <input type="file" name="image" class="form-control-file">
                  </div>

当我使用middleware.js标题不插入图像时,当用户不上传图像文件并返回TypeError时,给我此错误:无法读取null的属性“ image”     在module.exports(/home/mohamedessam/Desktop/NodeJs/middleware/storePosts.js:2:20)     在Layer.handle [作为handle_request](/home/mohamedessam/Desktop/NodeJs/node_modules/express/lib/router/layer.js:95:5)     在trim_prefix(/home/mohamedessam/Desktop/NodeJs/node_modules/express/lib/router/index.js:317:13)     在/home/mohamedessam/Desktop/NodeJs/node_modules/express/lib/router/index.js:284:7     在Function.process_params(/home/mohamedessam/Desktop/NodeJs/node_modules/express/lib/router/index.js:335:12)     在下一个(/home/mohamedessam/Desktop/NodeJs/node_modules/express/lib/router/index.js:275:10)     在urlencodedParser(/home/mohamedessam/Desktop/NodeJs/node_modules/body-parser/lib/types/urlencoded.js:100:7)     在Layer.handle [作为handle_request](/home/mohamedessam/Desktop/NodeJs/node_modules/express/lib/router/layer.js:95:5)     在trim_prefix(/home/mohamedessam/Desktop/NodeJs/node_modules/express/lib/router/index.js:317:13)     在/home/mohamedessam/Desktop/NodeJs/node_modules/express/lib/router/index.js:284:7     在Function.process_params(/home/mohamedessam/Desktop/NodeJs/node_modules/express/lib/router/index.js:335:12)     在下一个(/home/mohamedessam/Desktop/NodeJs/node_modules/express/lib/router/index.js:275:10)     在jsonParser(/home/mohamedessam/Desktop/NodeJs/node_modules/body-parser/lib/types/json.js:119:7)     在Layer.handle [作为handle_request](/home/mohamedessam/Desktop/NodeJs/node_modules/express/lib/router/layer.js:95:5)     在trim_prefix(/home/mohamedessam/Desktop/NodeJs/node_modules/express/lib/router/index.js:317:13)     在/home/mohamedessam/Desktop/NodeJs/node_modules/express/lib/router/index.js:284:7。

2 个答案:

答案 0 :(得分:2)

那是因为您总是直接检查子属性image,但是在某些时候您没有设置嵌套的req.files。由于req.files不存在,因此它会返回javascript无法解析的null类型。

您首先需要检查所有父键是否在javascript中分别存在,以测试对象中嵌套键的存在。以下应解决此问题:

if (!(req.files && req.files.image))

答案 1 :(得分:-1)

由于javascript区分大小写,因此您应提供图片格式中使用的确切名称