无法读取未定义的nodejs的属性“path”

时间:2014-06-07 13:47:42

标签: javascript node.js file curl

这是我的服务器端代码:

var fs = require('fs'),
MongoClient = require('mongodb').MongoClient, db;
var url = "mongodb://localhost:27017/fullwardrobedb";
MongoClient.connect(url, {native_parser: true}, function (err, connection) {
if (err) {
    console.log("Cannot connect to database " + url);
    process.exit(1);
}
db = connection;
});

fs.exists(__dirname + '/uploads', function(exists){
if(!exists){
    console.log('Creating dir ' + __dirname + '/uploads');
    fs.mkdir(__dirname + '/uploads', function(err){
        if(err){
            console.log('Error creating dir');
            process.exit(1);
        }
    })
}
});

exports.addPhoto = function(req,res,next){
console.log('Starting upload file');
console.log(req.files);
console.log(req.files.file);
var file = req.files.file,
    filePath = req.files.file.path,
    lastIndex = filePath.lastIndexOf("/"),
    tmpFileName = filePath.substr(lastIndex + 1),
    image = req.body,
    images = db.collection('images');

image.fileName = tmpFileName;
console.log('New file: ' + tmpFileName);

images.insert(image, function (err, result) {
    if (err) {
        console.log(err);
        return next(err);
    }
    res.json(image);
});
}

和server.js:

var express = require('express'),
    http = require('http'),
    path = require('path'),
    clothes = require('./routes/clothes'),
users = require('./routes/users');
photos = require('./routes/photos')

var app = express();

app.configure(function(){
app.use(express.logger('dev'));
//  app.use(express.bodyParser({
//        uploadDir:__dirname +  '/uploads',
//        keepExtensions: true
//    }));
app.use(express.json());
app.use(express.multipart({
    uploadDir:__dirname +  '/uploads',
    keepExtensions: true
}));
app.use(express.static(path.join(__dirname, './uploads')));
});

app.get('/clothes', clothes.findAll);
//app.get('/clothes/:id', clothes.findById);
// app.post('/clothes', clothes.addWine);
//app.put('/clothes/:id', clothes.updateWine);
//app.delete('/clothes/:id', clothes.deleteWine);

app.post('/users', users.addUser);
app.get('/users', users.findAll);
app.get('/users/:id', users.findById);
app.put('/users/:id', users.updateUser);

app.post('/photos', photos.addPhoto);

app.listen(3000,'192.168.1.8');
console.log('Listening on port 3000...');

问题在于,当我尝试使用命令curl -i -H "Content-Type:image/jpeg" -F filedata=@antoni_01.jpg http://192.168.1.8:3000/photos

放置卷曲图像时

我遇到了这样的例外:

Starting upload file
{}
undefined
TypeError: Cannot read property 'path' of undefined
    at exports.addPhoto (/Users/robert/dev/repos/fullwardrobe/fullwardrobe_nodejs/routes/photos.js:31:34)
    at callbacks (/Users/robert/dev/repos/fullwardrobe/fullwardrobe_nodejs/node_modules/express/lib/router/index.js:164:37)
    at param (/Users/robert/dev/repos/fullwardrobe/fullwardrobe_nodejs/node_modules/express/lib/router/index.js:138:11)
    at pass (/Users/robert/dev/repos/fullwardrobe/fullwardrobe_nodejs/node_modules/express/lib/router/index.js:145:5)
    at Router._dispatch (/Users/robert/dev/repos/fullwardrobe/fullwardrobe_nodejs/node_modules/express/lib/router/index.js:173:5)
    at Object.router (/Users/robert/dev/repos/fullwardrobe/fullwardrobe_nodejs/node_modules/express/lib/router/index.js:33:10)
    at next (/Users/robert/dev/repos/fullwardrobe/fullwardrobe_nodejs/node_modules/express/node_modules/connect/lib/proto.js:194:15)
    at Object.staticMiddleware [as handle] (/Users/robert/dev/repos/fullwardrobe/fullwardrobe_nodejs/node_modules/express/node_modules/connect/node_modules/serve-static/index.js:67:61)
    at next (/Users/robert/dev/repos/fullwardrobe/fullwardrobe_nodejs/node_modules/express/node_modules/connect/lib/proto.js:194:15)
    at Object.multipart [as handle] (/Users/robert/dev/repos/fullwardrobe/fullwardrobe_nodejs/node_modules/express/node_modules/connect/lib/middleware/multipart.js:97:43)]

有人可以帮助我吗?请注意我是Node JS的新手:)

1 个答案:

答案 0 :(得分:1)

问题是你覆盖了Content-Type标题。您基本上删除了Content-Type multipart/form-data的现有;type=image/jpeg,这是多部分中间件正在寻找以解析表单的内容。如果您需要明确设置文件字段的类型,可以在文件名后附加-F "filedata=@antoni_01.jpg;type=image/jpeg"(例如filedata)。

另一个问题是该文件的字段名称不匹配:file与{{1}}。