Node.js TypeError:无法读取属性'缩略图及#39; 未定义的

Node.js TypeError:无法读取属性'缩略图及#39; 未定义的,node.js,multer,Node.js,Multer,这是我的server.js文件 我正在尝试在服务器上查找上载图像。我知道多部分现在不起作用了 var config=require('./config'); var mongoose=require('mongoose'); var bodyparser=require('body-parser'); var express = require('express'); var morgan=require('morgan'); var nodemailer=require('nodemailer

这是我的server.js文件 我正在尝试在服务器上查找上载图像。我知道多部分现在不起作用了

var config=require('./config');
var mongoose=require('mongoose');
var bodyparser=require('body-parser');
var express = require('express');
var morgan=require('morgan');
var nodemailer=require('nodemailer');
var fs=require('fs');
var FB=require('fb');
var app = express();
app.use(bodyparser.urlencoded({extended:true}));
//app.use(express.bodyparser({uploadDir:'./uploads'})); //This is showing error.
app.use(bodyparser.json());
app.use(morgan('dev'));

app.use('/public',express.static(__dirname + '/public'));


mongoose.connect(config.database,function(err){
    if(err)
        console.log(err);
    else
        console.log('database connected');
});

app.listen(config.port,function(err){
    if(err)
        console.log(err);
    else
        console.log('server running at  '+config.port);

});

app.get('/',function(req,res){
    res.sendFile(__dirname +'/public/app/views/index.html' );
});
app.post('/file-upload', function(req, res) {
    // get the temporary location of the file
    var tmp_path = req.files.thumbnail.path;
    // set where the file should actually exists - in this case it is in the "images" directory
    var target_path = './public/images/' + req.files.thumbnail.name;
    // move the file from the temporary location to the intended location
    fs.rename(tmp_path, target_path, function(err) {
        if (err) throw err;
        // delete the temporary file, so that the explicitly set temporary upload dir does not get filled with unwanted files
        fs.unlink(tmp_path, function() {
            if (err) throw err;
            res.send('File uploaded to: ' + target_path + ' - ' + req.files.thumbnail.size + ' bytes');
        });
    });
});
这是我的表格:

<form method="post" enctype="multipart/form-data" action="/file-upload">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="file" name="thumbnail">
    <input type="submit">
</form>

现在如何上传图片?我第一次用这个。我开始知道穆特现在被使用了。但是在我的代码中如何以及在哪里使用它?

您正在使用的主体解析器中间件不支持多部分(文件上载)。它只支持JSON或url编码的表单提交。我要给你指出一个正确的方向。它谈论的是你正在讨论的同一个问题


解决方案:您需要支持多部分提交的不同中间件。

tmp\u path和
target\u path
的值是什么?tmp\u path是用户指定的上传文件的路径,target\u path是文件的最终存储位置。我知道了,但它们是否未定义?这是代码中唯一可以看到可能错误的地方。除非在前端有更多与表单一起使用的代码。如中未定义?定义为:var tmp_path=req.files.缩略图.path;如果您使用console.log
tmp\u路径
和/或
target\u路径
您的终端会说什么?
TypeError: Cannot read property &#39;thumbnail&#39; of undefined