I'm trying to upload an image and some data to a node.js server and saving it to parse.com. I'm also using https://github.com/danialfarid/angular-file-upload so my upload.js controller looks like this:
$scope.onFileSelect = function($files){
for (var i = 0; i < $files.length; i++) {
var file = $files[i];
$scope.images.push(file);
/**
* image preview logic
*/
var fileReader = new FileReader();
fileReader.readAsDataURL(file);
fileReader.onload = function(e) {
$timeout(function() {
$scope.imageSrc = e.target.result;
});
}
}
}
$scope.uploadProduct = function(){
var file = $scope.images[0];
$upload.upload({
url: Constants.API.baseUrl+'/products',
headers: {'Content-Type': file.type},
data: { product: $scope.product.info },
file: file
}).progress(function(ev) {
console.warn('percent: ' + parseInt(100.0 * ev.loaded / ev.total));
}).success(function(data) {
console.log(data);
$scope.$parent.productModal.hide();
}).error(function(data) {
console.error(data);
});
};
and the controller template:
<label class="item item-input item-stacked-label">
<span class="input-label">Image</span>
<input type="file"
ng-file-select="onFileSelect($files)"
multiple accept="image/*">
</label>
this is how i handle the node.js api:
function(req,res){
var success = function(product){
return res.status(200).json({
payload : product,
message : "api.products.create success"
});
}
var error = function(error){
return res.status(400).json({
error : error,
message : "api.products.create error"
});
}
var user = req.user;
var promise = new Parse.Promise();
var filename = new Buffer(24);
var data = req.files.file;
console.log(req.files)
_.times(24, function(i) {
filename.set(i, _.random(0, 255));
});
var parseFile = new Parse.File(user.id+"-"+filename.toString('base64'), data);
console.log(parseFile);
parseFile.save().then(function(file){
var ProductImage = Parse.Object.extend('ProductImage');
var image = new ProductImage();
image.set('filename', file.name);
image.set('url', file.url);
image.set('product', productObj);
image.save(null,{
success : success,
error: error
});
}, error);
}
parseFile console.log shows something like this: {_name: 'XXXXXXXX-KAJDHKAJSHDKJHASDKHSJSJS'}
and the error message is the following: "Cannot call method 'then' of undefined"
useful links: Parse.File Doc
any idea what I'm doing wrong?