Skip to content

(Bug) fetched object from server returns undefined

There is a lot of code involved in this process, so I will explain the best way possible to narrow down the problem. First, the user signs up and sends an object with other nested objects and arrays to the middleware. The middleware…

app.post('/api/passData', (req, res) =>
{
const {data} = req.body;

console.log(`passData returned: ${data.members[0]}`);

  dataPool.setData({data});
  
res.json(`${data} block was added`);
});

the console gets logged “passData returned: [object Object]”

this then gets processed into a “block” to be added to a block chain, with an array property named data that contains an object, deeper in this object eventually it contains the eccrypto https://github.com/bitchan/eccrypto public key. I need to access the public key but it keeps returning undefined (public key is almost certainly not the issue but for context).

Upon login, the user fetches the blockchain and the code narrows down the desired data depending on form input (form + processing form code may not be relevant).

I set a variable equal to field of chain[i].data[0].members[0].user which was derived by fetching the blockchain from the server (chain being the blockchain with different blocks in its indexes, after processing the targeted block it will access its data array with an object that has a “name” key and a members array where it stores all the user objects in its indexes.)

narrowed issue in login backend:

let tester = await decryptMes(chain[i].data[0].members[0].user);
//user public key is passed to data in decryptMes() parameters.
window.decryptMes = async function(data)
{
    var skey = getSKey();

    if (skey === null || undefined) 
    {
      console.log('You do not have a key pair');
      return;
    }
    console.log("skey is not null");
    console.log(`data returned ${data}`); //data is returning undefined!
    var decryptedMes = await eccrypto.decrypt(skey, data);
    var deMes = decryptedMes.toString();
    console.log(deMes);
    return deMes;
}

and in the decryptmes function, the debug string on line 11 returns “data returned undefined”

Important console messages:

data returned undefined
genKey.js:33886 Uncaught (in promise) TypeError: Cannot read property 'ephemPublicKey' of undefined
    at Object.exports.decrypt (genKey.js:33886)
    at window.decryptMes (genKey.js:26722)
    at window.search (Login.js:68)

notes: When I fetch the blockchain on postman, it shows that there is an object in the “members” array, how can it show up on postman but be undefined when I try printing it to console within encryptMes function or in any scope of the login function?

Postman shows:

    {
        "timestamp": 1612469806548,
        "lastHash": "0e01a641613ffa5518a8998267d07057cfd77eb60e99bb2b803e2e96ec118f86",
        "hash": "021472c81fe604e052ae108dd10fd9204daa0dcb7dc8f7ba33648e0deb48e2af",
        "data": [
            {
                "name": "LifeNet",
                "members": [
                    {
                        "0": {
                            "user": "[object Object]",
                            "profilePic": null,
                            "enDOB": "[object Object]",
                            "listeners": [],
                            "listening": [],
                            "friends": {},
                            "requested": [],
                            "blocked": [],
                            "channel": false
                        }
                    }
                ]
            }
        ],
        "nonce": 3,
        "difficulty": 3,
        "type": "pass Value here"
    }

as you can see, in index 0 there is the first member object indexed as 0 and its object field’s user is not undefined.

conclusion: What am I doing wrong for the object to be undefined? I hope I was able to effectively show what was relevant and any help will be greatly appreciated as I have lots to learn in JavaScript. Thank you

Below you can check out the login client backend, if you need to

login backend:

window.decryptMes = async function(data)
{
    var skey = getSKey();

    if (skey === null || undefined) 
    {
      console.log('You do not have a key pair');
      return;
    }
    console.log("skey is not null");
    console.log(`data returned ${data}`); //data is returning undefined!
    var decryptedMes = await eccrypto.decrypt(skey, data);
    var deMes = decryptedMes.toString();
    console.log(deMes);
    return deMes;
}
window.getData = async function()
{
var response = await fetch("https://goldengates.club:3000/api/blocks");
var chain = await response.json(); 
return chain;
}

window.login = async function(inputs)
{
  const chain = await getData(); //might need to parse this
  search(inputs,chain);
  console.log(chain);
}

window.search = async function(inputs,chain)
{
  console.log("search: entered");
  var username = inputs.user.value;

  var enUser = await encryptMes(username);

  console.log(enUser);

  console.log(username);
  var user;
  var uData;
  
  console.log("block loop: pre"); //stopping here?
  for(let i = chain.length-1; i> 0; i--) //genesis block minus 1 is not greater than 0
  {
    console.log("block loop: start");
    if('name' in chain[i].data[0])
    {
    console.log("name: looped");
    if(chain[i].data[0].name == `LifeNet`) //works
    {
      console.log(`${chain[i].data[0].name}`);
      console.log("name: entered");

      //below, what is being decrypted is undefined 
      let tester = await decryptMes(chain[i].data[0].members[0].user); 
      
      if(username == tester) //username instea of enUser .members[enUser]
      {
        console.log("user: looped");
        user = chain[i].data[0].members[enUser];
        //separate the keys and decrypt them here.
        
        
          var pfp = user.profilePic;
          var dob = user.enDOB;
          var lers = user.listeners;
          var ling = user.listening;
          var fnds = user.friends;
          var req = user.requested;
          var bck = user.blocked;
          var cnl = user.channel;

          var uData = 
          {
            username,
            pfp,
            dob,
            lers,
            ling,
            fnds,
            req,
            bck,
            cnl
          }
        
          localStorage.setItem('UD',JSON.stringify(uData));
          //window.location.href = "home.html";
        //redirect to home page with above data
        console.log(uData + "search: exited t");
        return uData;
        
      }
 
    }
  }
  }
  alert(`${username} is not registered`);
  console.log(username);
  console.log("search: exited f");
  return false;
}

Answer

based on the object in the question for accessing the user should using members[0][0] so just try

chain[i].data[0].members[0][0].user