TypeError:无法在Object.Parse.File.save中调用undefined方法'then'

时间:2013-07-08 02:20:57

标签: javascript angularjs parse-platform promise

我从Angular js应用程序使用js api for parse.com。我正在尝试保存/更新用户的个人资料图片。我有以下代码:

一些HTML。 。

<input type="file" capture="camera" accept="image/*" id="profilePhotoFileUpload">

Thansk到google i/oraymond以及parse js guide

我控制器中的代码:

$scope.updateUserProfile = function (user) {

        var fileUploadControl = $("#profilePhotoFileUpload")[0];
        if (fileUploadControl.files.length > 0) {
            var file = fileUploadControl.files[0];
            var name = "photo.jpg";

            var parseFile = new Parse.File(name, file);

            parseFile.save();


}

我一直收到这个错误:

TypeError:无法调用未定义的方法'then'     在Object.Parse.File.save(parse-1.2.8.js:4084:43)

enter image description here

调用parseFile.save()

基本上_source是未定义的。 。 。为什么?!

谢谢!

2 个答案:

答案 0 :(得分:1)

我终于解决了这个问题,因为我的最终目标是将其与phonegap with the info in this post.一起使用。非常感谢Raymond Camden!

函数gotPic(data){

window.resolveLocalFileSystemURI(data, function(entry) {

    var reader = new FileReader();

    reader.onloadend = function(evt) {
        var byteArray = new Uint8Array(evt.target.result);
        var output = new Array( byteArray.length );
        var i = 0;
        var n = output.length;
        while( i < n ) {
            output[i] = byteArray[i];
            i++;
        }                
        var parseFile = new Parse.File("mypic.jpg", output);

        parseFile.save().then(function(ob) {
                navigator.notification.alert("Got it!", null);
                console.log(JSON.stringify(ob));
            }, function(error) {
                console.log("Error");
                console.log(error);
            });

    }

    reader.onerror = function(evt) {
          console.log('read error');
          console.log(JSON.stringify(evt));
      }

    entry.file(function(s) {
        reader.readAsArrayBuffer(s);
    }, function(e) {
        console.log('ee');
    });

});

}

答案 1 :(得分:1)

如果其他人在使用Phonegap和Parse,这是Raymond Camden的另一个有用的例子,它拍了一张照片:

http://www.raymondcamden.com/2013/07/23/better-example-of-phonegap-parse-and-uploading-files

var imagedata = "";    

$("#takePicBtn").on("click", function(e) {
    e.preventDefault();
    navigator.camera.getPicture(gotPic, failHandler, 
        {quality:50, destinationType:navigator.camera.DestinationType.DATA_URL,
         sourceType:navigator.camera.PictureSourceType.PHOTOLIBRARY});
});

function gotPic(data) {
    console.log('got here');
    imagedata = data;
    $("#takePicBtn").text("Picture Taken!").button("refresh");
}

它会像以下一样保存:

var parseFile = new Parse.File("mypic.jpg", {base64:imagedata});
        console.log(parseFile);
            parseFile.save().then(function() {
                var note = new NoteOb();
                note.set("text",noteText);
                note.set("picture",parseFile);
                note.save(null, {
                    success:function(ob) {
                        $.mobile.changePage("#home");
                    }, error:function(e) {
                        console.log("Oh crap", e);
                    }
                });
                cleanUp();
            }, function(error) {
                console.log("Error");
                console.log(error);
            });

特别注意{base64:imagedata},因为这是使用这样的字符串数据创建解析文件的关键。

相关问题