简体   繁体   English

PhoneGap上传照片:无法读取未定义的属性“文件”

[英]PhoneGap upload photos: Cannot read property 'file' of undefined

I'm trying to create an application that will upload a photo to a mongodb server, here is my code: 我正在尝试创建一个将照片上传到mongodb服务器的应用程序,这是我的代码:

   upload = function (imageURI) {
        var ft = new FileTransfer(),
            options = new FileUploadOptions();

        options.fileKey = "file";
        options.fileName = 'filename.jpg'; Node at the server side.
        options.mimeType = "image/jpeg";
        options.chunkedMode = false;
        options.params = { 
            "description": "Uploaded from my phone"
        };

        // alert(imageURI);
        // alert(serverURL);

        ft.upload(imageURI, serverURL + "/images",
            function (e) {
                getFeed();
            },
            function (e) {
                alert("Upload failed");
            }, options);
    }

and on the node server side this is where the error seems to happen: 在节点服务器端,这似乎是发生错误的地方:

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

    image.fileName = tmpFileName;
    console.log(tmpFileName);

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

};

so I'm having the error Cannot read property 'file' of undefined 所以我遇到了无法读取未定义的属性“文件”的错误

I think it is an error related to the version of the express that you are using. 我认为这是与您使用的Express版本有关的错误。 In previous version of express (3.x) you should include : 在Express(3.x)的早期版本中,您应该包括:

app.use(express.methodOverride()); app.use(express.multipart());

so you can access to req.files 这样您就可以访问req.files

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法读取文件上传的未定义属性“ 0” - Cannot read property '0' of undefined for File Upload 未捕获的TypeError:无法读取未定义的属性“照片” - Uncaught TypeError: Cannot read property 'photos' of undefined ng-file-upload无法读取未定义的属性'file' - ng-file-upload cannot read property 'file' of undefined PhoneGap Camera,无法读取未定义的属性'getPicture' - PhoneGap Camera , Cannot read property 'getPicture' of undefined TypeError:无法读取Angular 7上载文件中未定义错误的属性“ post” - TypeError: Cannot read property 'post' of undefined error on Angular 7 Upload file 尝试上传图像时无法读取 express 中未定义的属性“文件” - Cannot read property 'file' of undefined in express when trying to upload image 无法读取未定义的属性“文件” - Cannot read property 'file' of undefined 上传图片,无法读取未定义的属性“订阅” - Upload image, Cannot read property 'subscribe' of undefined Phonegap Camera API-无法读取未定义的属性“ DATA_URL” - Phonegap Camera API - Cannot read property 'DATA_URL' of undefined Phonegap构建-未捕获的TypeError:无法读取未定义的属性'getPicture' - Phonegap build - Uncaught TypeError: Cannot read property 'getPicture' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM