Return CID of a file

Hi everyone,
I am currently developing a simple file-uploader using js-ipfs.
Everything works correctly, I can upload the file to IPFS and access it using the CID.
I have a problem though, probably code-related; the part in which I am having this problem is the following:
const addFile = async (fileName, filePath) => {
const fileContent = fs.readFileSync(filePath);
console.log('fileBuffer: ', fileContent);

const fileAdded = await ipfs.add({ path: fileName, content: fileContent });
console.log('Your file on IPFS: ', fileAdded);

const fileHash = fileAdded.cid;
console.log('Your hash on IPFS: ', fileHash);

return fileHash;

};
Basically, with console.log of fileAdded I can see the details of the upload file (including the CID) as:
Your file on IPFS: [
{
path: ‘Test’,
hash: ‘Qmxxxxx’,
size: 107024
}
]
but when I specifically try to print the cid value using fileAdded.cid or fileAdded.hash, the hash value is undefined.
Does anybody know why? How can I return the CID correctly after having added the file to IPFS?
Thank you all

This is exactly the kind of issues I run into, the ipfs-docs need to be updated I guess. What I found out is that the object returned by ipfs.add ( your const fileAdded) has the following return:

Object { code: 112, version: 0, multihash: {…}, bytes: Uint8Array(34), … }
_baseCache: Map { z → "QmZ5KYSUgWSaRbo2ctFGHSts7Ns9yfsU9sHa4MQvqk2ocg" }
asCID: Object { code: 112, version: 0, multihash: {…}, … }
byteLength: 34
byteOffset: 0
bytes: Uint8Array(34) [ 18, 32, 159, … ]
code: 112
multihash: Object { code: 18, size: 32, digest: Uint8Array(32), … }
version: 0
<prototype>: Object { … }

When you add .byteLength for instance (fileAdded.byteLength) it should give 34 (this is just an example, not the real result of your code question)
But the strange thing is, when you add .toString() it returns just the clean cid.
So I hope this works for you,
:tophat:
This is what I found out just by trying things over and over, I will edit this tiny part on the docs on github, but hopefully the coders can find some time to explain how to use the updated (multiformats) js-ipfs.
PS: I don’t understand why adding .cid gives the object you described in your question (should be .asCID right!?) , I hope someone can explain it

Personally I solved the problem by using “fileAdded[0].hash”. I guess that ipfs.add returns that kind of array so, since I’m uploading only one file at the time, I just need to access the first element of the array.
However, I agree that the documentation should be updated since it’s very difficult to find the correct information and instructions nowadays