Gett “Cannot read property of 0” when trying to access an array point
up vote
0
down vote
favorite
I'm trying to access a value set in a function in another function, then I want to return this value at the end, I've got a console.log to show the value and the log shows the value just fine, but when trying to set it I get an error stating: TypeError: Cannot read property '0' of undefined
It seems that when I try to return the value (I do this by setting the function as a variable) a fucntion with no result is shown. Not sure how I can show the result form my setValue function, any ideas? :) My express code:
function showBalance(cardNumber) {
connection.query(
"SELECT Balance from userCards WHERE CardNumber = '" + cardNumber + "'",
function(err, rows) {
if (err) {
throw err;
} else {
setValue(rows);
}
}
);
function setValue(value) {
console.log('Balance object is:', value[0].Balance);
const ownBalance = value[0].Balance;
console.log('The user balance is: ', ownBalance);
return ownBalance;
}
const returnBalance = setValue();
return returnBalance;
}
module.exports = showBalance;
javascript express
add a comment |
up vote
0
down vote
favorite
I'm trying to access a value set in a function in another function, then I want to return this value at the end, I've got a console.log to show the value and the log shows the value just fine, but when trying to set it I get an error stating: TypeError: Cannot read property '0' of undefined
It seems that when I try to return the value (I do this by setting the function as a variable) a fucntion with no result is shown. Not sure how I can show the result form my setValue function, any ideas? :) My express code:
function showBalance(cardNumber) {
connection.query(
"SELECT Balance from userCards WHERE CardNumber = '" + cardNumber + "'",
function(err, rows) {
if (err) {
throw err;
} else {
setValue(rows);
}
}
);
function setValue(value) {
console.log('Balance object is:', value[0].Balance);
const ownBalance = value[0].Balance;
console.log('The user balance is: ', ownBalance);
return ownBalance;
}
const returnBalance = setValue();
return returnBalance;
}
module.exports = showBalance;
javascript express
You're callingsetValue()without passing in any value just before the return statement. The code can't accessvalue[0]because value is not set. Do you need to set the value? Can you not justreturn rows[0].Balance;wheresetValue(rows);currently is?
– Tim Rooke
Nov 7 at 17:50
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to access a value set in a function in another function, then I want to return this value at the end, I've got a console.log to show the value and the log shows the value just fine, but when trying to set it I get an error stating: TypeError: Cannot read property '0' of undefined
It seems that when I try to return the value (I do this by setting the function as a variable) a fucntion with no result is shown. Not sure how I can show the result form my setValue function, any ideas? :) My express code:
function showBalance(cardNumber) {
connection.query(
"SELECT Balance from userCards WHERE CardNumber = '" + cardNumber + "'",
function(err, rows) {
if (err) {
throw err;
} else {
setValue(rows);
}
}
);
function setValue(value) {
console.log('Balance object is:', value[0].Balance);
const ownBalance = value[0].Balance;
console.log('The user balance is: ', ownBalance);
return ownBalance;
}
const returnBalance = setValue();
return returnBalance;
}
module.exports = showBalance;
javascript express
I'm trying to access a value set in a function in another function, then I want to return this value at the end, I've got a console.log to show the value and the log shows the value just fine, but when trying to set it I get an error stating: TypeError: Cannot read property '0' of undefined
It seems that when I try to return the value (I do this by setting the function as a variable) a fucntion with no result is shown. Not sure how I can show the result form my setValue function, any ideas? :) My express code:
function showBalance(cardNumber) {
connection.query(
"SELECT Balance from userCards WHERE CardNumber = '" + cardNumber + "'",
function(err, rows) {
if (err) {
throw err;
} else {
setValue(rows);
}
}
);
function setValue(value) {
console.log('Balance object is:', value[0].Balance);
const ownBalance = value[0].Balance;
console.log('The user balance is: ', ownBalance);
return ownBalance;
}
const returnBalance = setValue();
return returnBalance;
}
module.exports = showBalance;
javascript express
javascript express
asked Nov 7 at 17:42
sam_vimes
285
285
You're callingsetValue()without passing in any value just before the return statement. The code can't accessvalue[0]because value is not set. Do you need to set the value? Can you not justreturn rows[0].Balance;wheresetValue(rows);currently is?
– Tim Rooke
Nov 7 at 17:50
add a comment |
You're callingsetValue()without passing in any value just before the return statement. The code can't accessvalue[0]because value is not set. Do you need to set the value? Can you not justreturn rows[0].Balance;wheresetValue(rows);currently is?
– Tim Rooke
Nov 7 at 17:50
You're calling
setValue() without passing in any value just before the return statement. The code can't access value[0] because value is not set. Do you need to set the value? Can you not just return rows[0].Balance; where setValue(rows); currently is?– Tim Rooke
Nov 7 at 17:50
You're calling
setValue() without passing in any value just before the return statement. The code can't access value[0] because value is not set. Do you need to set the value? Can you not just return rows[0].Balance; where setValue(rows); currently is?– Tim Rooke
Nov 7 at 17:50
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
In the line const returnBalance = setValue() you call setValue with 0 arguments. This will cause the script to fail, as you cannot do value[0] when value is undefined.
You can't make your outer function return a value that comes from the anonymous inner callback. This is because the outer function returns before the callback is invoked. You need to do one of the following:
Pass a function as an argument to your outer function that represents what should happen after the callback fires. You call this function from the callback and pass the value to it. This is called continuation passing style.
Use an API that supports Promises, and have your outer function return a Promise containing the value instead of the value itself. You can adapt a callback based API to promises using promisify: https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original
Use async/await to write your asynchronous code in a style that is very similar to synchronous code. This also uses Promises under the hood, so it requires a compatible API or promisify as above.
How do I set the value? I can't just return the value of the function in the parent function as it is out of scope
– sam_vimes
Nov 7 at 17:51
@sam_vimes Please see edit
– Jonas Høgh
Nov 7 at 19:40
I saw the edit, your info seems good I'm just spending allot of time trying to figure out how to do this following your advice.
– sam_vimes
Nov 8 at 11:41
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
In the line const returnBalance = setValue() you call setValue with 0 arguments. This will cause the script to fail, as you cannot do value[0] when value is undefined.
You can't make your outer function return a value that comes from the anonymous inner callback. This is because the outer function returns before the callback is invoked. You need to do one of the following:
Pass a function as an argument to your outer function that represents what should happen after the callback fires. You call this function from the callback and pass the value to it. This is called continuation passing style.
Use an API that supports Promises, and have your outer function return a Promise containing the value instead of the value itself. You can adapt a callback based API to promises using promisify: https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original
Use async/await to write your asynchronous code in a style that is very similar to synchronous code. This also uses Promises under the hood, so it requires a compatible API or promisify as above.
How do I set the value? I can't just return the value of the function in the parent function as it is out of scope
– sam_vimes
Nov 7 at 17:51
@sam_vimes Please see edit
– Jonas Høgh
Nov 7 at 19:40
I saw the edit, your info seems good I'm just spending allot of time trying to figure out how to do this following your advice.
– sam_vimes
Nov 8 at 11:41
add a comment |
up vote
0
down vote
In the line const returnBalance = setValue() you call setValue with 0 arguments. This will cause the script to fail, as you cannot do value[0] when value is undefined.
You can't make your outer function return a value that comes from the anonymous inner callback. This is because the outer function returns before the callback is invoked. You need to do one of the following:
Pass a function as an argument to your outer function that represents what should happen after the callback fires. You call this function from the callback and pass the value to it. This is called continuation passing style.
Use an API that supports Promises, and have your outer function return a Promise containing the value instead of the value itself. You can adapt a callback based API to promises using promisify: https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original
Use async/await to write your asynchronous code in a style that is very similar to synchronous code. This also uses Promises under the hood, so it requires a compatible API or promisify as above.
How do I set the value? I can't just return the value of the function in the parent function as it is out of scope
– sam_vimes
Nov 7 at 17:51
@sam_vimes Please see edit
– Jonas Høgh
Nov 7 at 19:40
I saw the edit, your info seems good I'm just spending allot of time trying to figure out how to do this following your advice.
– sam_vimes
Nov 8 at 11:41
add a comment |
up vote
0
down vote
up vote
0
down vote
In the line const returnBalance = setValue() you call setValue with 0 arguments. This will cause the script to fail, as you cannot do value[0] when value is undefined.
You can't make your outer function return a value that comes from the anonymous inner callback. This is because the outer function returns before the callback is invoked. You need to do one of the following:
Pass a function as an argument to your outer function that represents what should happen after the callback fires. You call this function from the callback and pass the value to it. This is called continuation passing style.
Use an API that supports Promises, and have your outer function return a Promise containing the value instead of the value itself. You can adapt a callback based API to promises using promisify: https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original
Use async/await to write your asynchronous code in a style that is very similar to synchronous code. This also uses Promises under the hood, so it requires a compatible API or promisify as above.
In the line const returnBalance = setValue() you call setValue with 0 arguments. This will cause the script to fail, as you cannot do value[0] when value is undefined.
You can't make your outer function return a value that comes from the anonymous inner callback. This is because the outer function returns before the callback is invoked. You need to do one of the following:
Pass a function as an argument to your outer function that represents what should happen after the callback fires. You call this function from the callback and pass the value to it. This is called continuation passing style.
Use an API that supports Promises, and have your outer function return a Promise containing the value instead of the value itself. You can adapt a callback based API to promises using promisify: https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original
Use async/await to write your asynchronous code in a style that is very similar to synchronous code. This also uses Promises under the hood, so it requires a compatible API or promisify as above.
edited Nov 7 at 19:40
answered Nov 7 at 17:48
Jonas Høgh
7,07011736
7,07011736
How do I set the value? I can't just return the value of the function in the parent function as it is out of scope
– sam_vimes
Nov 7 at 17:51
@sam_vimes Please see edit
– Jonas Høgh
Nov 7 at 19:40
I saw the edit, your info seems good I'm just spending allot of time trying to figure out how to do this following your advice.
– sam_vimes
Nov 8 at 11:41
add a comment |
How do I set the value? I can't just return the value of the function in the parent function as it is out of scope
– sam_vimes
Nov 7 at 17:51
@sam_vimes Please see edit
– Jonas Høgh
Nov 7 at 19:40
I saw the edit, your info seems good I'm just spending allot of time trying to figure out how to do this following your advice.
– sam_vimes
Nov 8 at 11:41
How do I set the value? I can't just return the value of the function in the parent function as it is out of scope
– sam_vimes
Nov 7 at 17:51
How do I set the value? I can't just return the value of the function in the parent function as it is out of scope
– sam_vimes
Nov 7 at 17:51
@sam_vimes Please see edit
– Jonas Høgh
Nov 7 at 19:40
@sam_vimes Please see edit
– Jonas Høgh
Nov 7 at 19:40
I saw the edit, your info seems good I'm just spending allot of time trying to figure out how to do this following your advice.
– sam_vimes
Nov 8 at 11:41
I saw the edit, your info seems good I'm just spending allot of time trying to figure out how to do this following your advice.
– sam_vimes
Nov 8 at 11:41
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53194901%2fgett-cannot-read-property-of-0-when-trying-to-access-an-array-point%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You're calling
setValue()without passing in any value just before the return statement. The code can't accessvalue[0]because value is not set. Do you need to set the value? Can you not justreturn rows[0].Balance;wheresetValue(rows);currently is?– Tim Rooke
Nov 7 at 17:50