1

This is the getFile function I wrote. Using it-to-buffer to access file chunks.

async function getFile(cid){

    let content = await toBuffer(client.cat(cid));
    return content;
}

const getFileFromNet = async(cid)=>{
    return await getFile(cid);
}

This is the server.js code referencing the exported module getFileFromNet. I am using express.js and firebase.database

app.post("/profile", async function (req, res){
const current_user = req.body;
const uid = current_user.uid;
console.log("User ID: "+uid);
db.ref("user_file_ref").on("value",snapshot=>{
   snapshot.forEach(dataSnapshot=>{
       if(uid == dataSnapshot.val().user_id){
           let loggable = getFileFromNet(dataSnapshot.val().ipfs_ref);
           console.log(loggable) ;
       }
   })
 })
});

The console log is as follows

  • User ID: w25eDefrvuaq5SNzjYGk6MQrfZf2
  • Promise { }
  • Promise { }
  • `getFileFromNet`, like all `async` functions, returns a promise. You have to consume the promise to get its result. Inside an `async` function, you do that with `await`. Outside an `async` function, you do that with `.then` and `.catch`. See the [linked question](https://stackoverflow.com/questions/29516390/how-to-access-the-value-of-a-promise) for further details. – T.J. Crowder Apr 16 '21 at 07:19
  • I am handling it accordingly but I still get a promise object – chipego kalinda Apr 16 '21 at 08:20
  • No you aren't. You have: `let loggable = getFileFromNet(dataSnapshot.val().ipfs_ref); console.log(loggable) ;` That logs a promise because `loggable` is a promise, because all `async` functions return promises. – T.J. Crowder Apr 16 '21 at 08:20
  • I changed it to `let loggable = getFileFromNet(dataSnapshot.val().ipfs_ref).then(result =>{return result;}).catch(err =>{console.log(err)});` and I still have the same result. I even went further to do `let real_loggable = loggable.then(value =>{return value}).catch(err =>{console.log(err)})` and I have the same result. – chipego kalinda Apr 16 '21 at 08:26
  • Please read the [linked question's answers](https://stackoverflow.com/questions/29516390/how-to-access-the-value-of-a-promise). You can't use the result synchronously, no matter how many `.then`s you add. Use the fulfillment value **inside** the `.then` callback. See also: http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – T.J. Crowder Apr 16 '21 at 08:32
  • 1
    Thanks. I understand now. – chipego kalinda Apr 16 '21 at 10:01

0 Answers0