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;









share|improve this question






















  • 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

















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;









share|improve this question






















  • 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















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;









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 17:42









sam_vimes

285




285












  • 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


















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














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.







share|improve this answer























  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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.







share|improve this answer























  • 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















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.







share|improve this answer























  • 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













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.







share|improve this answer














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.








share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings