node.jsjs-ipfs

ipfs.files.add not awaiting


I have a function in node.js that adds a file to IPFS, takes the hash and ggives it to a function to generate qrcode using the hash. But my qrcode is being produced before the file is uploaded, and await is not helping.

Please help!

await ipfs.files.add(testBuffer, function (err, file) {
        if (err) {
          console.log(err);
        }
        {filehash=file;
        console.log(filehash);
        console.log("printed filehash");
        
      }})

      const qrcodepic= await qr.generateQRCode(filehash,EmpName,EmpId,IDate);

      console.log(qrcodepic);
      return qrcodepic;

Solution

  • The await did not work as I was adding a callback frunction also. It works fine if you don't include the callback function. ie, do it like

    fileHash=await ipfs.files.add(testBuffer);