2
votes

I'm trying to integrate Cloudinary in my project for uploading images. I have just started using the following links:

http://cloudinary.com/documentation/node_image_upload https://github.com/cloudinary/cloudinary_npm/blob/master/samples/basic/basic.js

I'm stuck at a point where I can see the image uploaded to Cloudinary in my dashboard but in my code the response is undefined.

Code:

cloudinary.config(config.cloudinaryConfig);
function uploadFileInCloudinary(fileObj, callback) {
    console.log(fileObj);
    if(fileObj && (fileObj != undefined)) {
        console.log(fileObj['path']);
        cloudinary.uploader.upload(fileObj['path'], function(err, result) {
            if(err) {
                callback(err);
            }
            console.log("cloudinary result");
            console.log(JSON.stringify(result));  // undefined
            callback(null, result);
        });
    } else {
        callback("error");
    }
}

Why is it so? Can anyone give me some pointers to where am I doing wrong?

1
You are missing return before callback(err), is it possible that you are missing error response by callback being called twice?Paweł Sroka
@PawełSroka: Adding return statement solved my problem. Thanks a lot :)dark_shadow

1 Answers

0
votes

By default, uploading is performed synchronously. Once finished, the uploaded image or video is immediately available for manipulation and delivery. An upload call returns,

You need to define a return statement before you initiate a callback response

doing this should take away the undefined error