Get 'undefined' in this variable reference, how do I solve it

I’ve tried to match if two data come from the same source by attached a user id to them. But I can’t check if they were coming from the same source with this function. How do I solve it:

 var a = Math.floor((Math.random()*100)+1);
 var x = function() {
     var b = [a, 555];   
     return b;
 }
 var y = function() {
     var d = { e: a, f: 555 },
         g = JSON.stringify({ type: 'test', data: d });
     return g;
 }

 function callback(i) {
     var v;
 	if (typeof i === 'object') {
         var w = i,
             v = w[0];
     }
 	if (typeof i === 'string') {
         var x = i,
             y = JSON.parse(x),
             z = y.data.e;
             console.log(v); // problem is here
     }
 }

 var test = document.getElementById('test');
     test.addEventListener('click', function() {
         callback(x());
 		callback(y());
     });

Note: there is no way I call it this way: callback(x(), y());

Thank you,

v is defined only if i is an object, but since i is a string, v is undefined.

Thanks,
Is there any way to make this possible?

If you want v to have a value within this code then you need to assign something to it.

Hi @felgall,

Exactly my thought, but how. I try and error for two days now.

This is way too vague brotha, it’s hard to understand exactly what you’re trying to do especially since the variables are just random letters.

The way your function is setup, y() returns a string and so therefore v is left undefined. v is only set when callback gets passed an object, which only happens when callback(x())

Are you trying to compare if x() === y()?

It all depends on what value you want it to have. At the moment you are not assigning a value at all.

What are you going to use it for? At the moment you are setting values and then abandoning them so it is difficult to tell how you intend to use the values - particularly the ones you don’t even set.

@labofoz

In real life I send this data through websocket. A user’s activity will generate two type of data, say, an array and string. For string data, I convert it to JSON to send it over to a sever. The random number is user ID. I don’t know which user so I mock-up it as random number to imitate the real user id.

With ID, so I know who generate that data. But since data is generate separately and there is only:

ws.on('message', function() {
  // do something
});

on the server side to receive data, so I mock it up on the same page.

I try to match that those id to make sure that they come from the same user and not of the other user’s before I process the data.

Hope that’s clear.

Note: string seem to reach the server faster than an array, but even I reverse the order the result is still the same. Duh…

OK. I solve this:

var s, t, u;
function callback(i) {
	if (typeof i === 'string') {
        return t = i;
    }
	if (typeof i === 'object') {
        u = i;
    }
}

It looks like you have a limited character budget :wink:

So I have no idea at all what “s” “t” “u” “i” are supposed to be.

But if you want to return true why did you write it as return t = i ?

No, I don’t want it return true. I’ve no idea about that. I just want data. Here’s the working code:

var a = Math.floor((Math.random()*100)+1); var x = function() { var c = { d: a, e: 'klmn' }, f = JSON.stringify({ type: 'test', data: c }); return f; } var y = function() { var b = [a, 555]; return b; } var s, t, u; function callback(i) { if (typeof i === 'string') { return t = i; } if (typeof i === 'object') { u = i; } var s = JSON.parse(t), w = s.data.d, z = u[0]; if( w === z ) { console.log(u[1] + ' : ' + s.data.e); } } var test = document.getElementById('test'); test.addEventListener('click', function() { callback(x()); callback(y()); });

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.