This is a strange one that I can’t quite understand and I think that showing the code first will be best
component.ts
this.camera.getPicture(options).then(
(imageData: string): void => {
const mp4Checker = imageData.slice(-3);
if (mp4Checker === 'mp4') {
console.log('imageData', imageData); // <-- 'path/to/storage/file.mp4'
message.video = imageData;
console.log('after assignment', message.video); // <-- 'path/to/storage/file.mp4'
this.chatService.sendVideo(message);
} else {
console.log('imageData', imageData); //<-- 'string/to/storage/file.jpg'
message.image = imageData;
console.log('after assignment', message.image); // <-- undefined
this.chatService.sendMessage(message);
}
this._uploadMedia(imageData);
},
err => {
console.log('error -->', err);
},
);
now If I move the logic to separate function it fixes the undefined
problem:
this.camera.getPicture(options).then(
(imageData: string): void => {
this._uploadMedia(imageData);
},
err => {
console.log('error -->', err);
},
);
private _uploadMedia(media: string): void {
const mp4Checker = media.slice(-3);
console.log('_uploadMedia', media, mp4Checker);
if (mp4Checker === 'mp4') {
message.video = media;
console.log('look at message.video', message); // <--'path/to/storage/file.mp4'
} else {
message.image = media;
console.log('look at message.image', message); //<--'path/to/storage/file.jpg'
}
}
Why in the first example can I not assign it but after passing the reference to the helper function I am only then finally able to?