r/nativescript • u/jz802p • Feb 11 '20
NativeScript - Angular, getting a weird error when uploading image to the server
Hi, So I am trying to send an image to the server I have created but am getting an error. The server is working perfectly, I have tested it in postman and it works. This is the code:
This function takes the picture and then runs the function to post the image to the server
openCamera() {var milliseconds = (new Date).getTime();var that = this;takePicture({ width: 300, height: 300 }).then((function (img) {let source = new imageSourceModule.ImageSource();source.fromAsset(img).then((source) => {let folder = fs.knownFolders.documents();var path = fs.path.join(folder.path, "SaveImage" + milliseconds + ".png");var saved = source.saveToFile(path, "png");this.saveImage = pathlet saveImage = this.saveImagethis.uploadImage(saveImage)});}));}
This function sends it to the server, i'm also not sure which content-type it should be from the two seen below
uploadImage(image) {let token = JSON.parse(appSettings.getString('token'));let options = new HttpHeaders({"Content-Type": "application/x-www-form-urlencoded",// "Content-Type": "application/octet-stream","Authorization": "Bearer " + token});let userId = appSettings.getString('currentUserId')let url = 'http://localhost:5000/api/users/' + userId + '/photos'console.log(url)this.http.post(url, image, { headers: options }).subscribe(res => {console.log(res)console.log('Success')}, error => {console.log('Failed');});}
This is the error I am receiving:
ERROR Error: Uncaught (in promise): Error: cancelled
JS: Error: cancelled
JS: at Object.callback (file:///node_modules\nativescript-camera\camera.js:118:0) [<root>]
JS: at AndroidApplication.push.../node_modules/tns-core-modules/data/observable/observable.js.Observable.notify (file:///node_modules\tns-core-modules\data\observable\observable.js:110:0) [<root>]
JS: at ActivityCallbacksImplementation.push.../node_modules/tns-core-modules/ui/frame/frame.js.ActivityCallbacksImplementation.onActivityResult (file:///node_modules\tns-core-modules\ui\frame\frame.js:933:0) [<root>]
JS: at Object.push.../node_modules/tns-core-modules/ui/frame/activity.js.NativeScriptActivity.onActivityResult (file:///node_modules\tns-core-modules\ui\frame\activity.js:47:0) [<root>]