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

Posted

技术标签:

【中文标题】无法读取 null 的属性“图像”【英文标题】:Cannot read property 'image' of null 【发布时间】:2019-11-19 03:56:20 【问题描述】:

无法在 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')
)

创建.edge:

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

当我使用 middleware.js 标题不插入图像时,当用户不上传图像文件并返回 TypeError 时,它给我这个错误:无法读取属性 'image' of null 在 module.exports (/home/mohamedessam/Desktop/NodeJs/middleware/storePosts.js:2:20) 在 Layer.handle [as 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 [as 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 [as 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。

【问题讨论】:

req.files 为空,因此无法访问空对象的图像属性 【参考方案1】:

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

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

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

【讨论】:

【参考方案2】:

由于 javascript 是区分大小写的,因此您应该给出图像表单中使用的确切名称

【讨论】:

以上是关于无法读取 null 的属性“图像”的主要内容,如果未能解决你的问题,请参考以下文章

Gatsby 错误无法读取 null 的“固定”属性

slick.js 中的错误:“未捕获的 TypeError:无法读取属性 'add' of null”

无法读取 null 的属性(读取“类型”)

TypeError:无法读取 null 的属性(读取“发射”)

未捕获的类型错误:无法读取 null 的属性(读取“添加”)

未捕获的类型错误无法读取 null 的属性(读取“查询选择器”)