I have two functions
async function convertToBase64(file) { const fileReader = new FileReader(); fileReader.onload = () => { const srcData = fileReader.result; console.log('scrData: ', srcData); // result is correct return srcData; }; fileReader.readAsDataURL(file); } async function addData(values) { const converted = await convertToBase64(values.file); console.log(converted); // result undefined await addDoc(collection(db, 'list'), { image: converted, }); }
I tried try...catch and async-await functions, but I couldn't find a solution anyway
The
convertToBase64()
function does not explicitly return an value, so the code you provide always returnsundefined
. You can change the function so that it returns a Promise once resolves " rel="nofollow noreferrer">FileReader Successfully read the file in base64 and handled anyreject
ions or occurrences of mistake: