Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法读取属性';图像';空的_Javascript_Node.js_Node Modules - Fatal编程技术网

Javascript 无法读取属性';图像';空的

Javascript 无法读取属性';图像';空的,javascript,node.js,node-modules,Javascript,Node.js,Node Modules,无法在module.exports处读取null的属性“image” 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

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

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时,它会给我这个错误:无法读取null的属性“image” 在module.exports(/home/mohamedessam/Desktop/NodeJs/middleware/storePosts.js:2:20) 在Layer.handle[作为handle_请求](/home/mohamedessam/Desktop/NodeJs/node_modules/express/lib/router/Layer.js:95:5) 在trim_前缀(/home/mohamedessam/Desktop/NodeJs/node_modules/express/lib/router/index.js:317:13) at/home/mohamedessam/Desktop/NodeJs/node_modules/express/lib/router/index.js:284:7 在Function.process_参数(/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_请求](/home/mohamedessam/Desktop/NodeJs/node_modules/express/lib/router/Layer.js:95:5) 在trim_前缀(/home/mohamedessam/Desktop/NodeJs/node_modules/express/lib/router/index.js:317:13) at/home/mohamedessam/Desktop/NodeJs/node_modules/express/lib/router/index.js:284:7 在Function.process_参数(/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_请求](/home/mohamedessam/Desktop/NodeJs/node_modules/express/lib/router/Layer.js:95:5) 在trim_前缀(/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。

这是因为您总是直接检查子属性
image
,但有时您没有设置嵌套它的
请求文件。由于
req.files
不存在,它返回一个null类型,javascript无法解析该类型

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

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

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

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

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

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

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

req.files
为null,因此您无法访问null对象的image属性
req.files
为null,因此,无法访问空对象的image属性