Reputation: 2515
Scratching my head. I'm getting an undefined error when trying to call a function. Thing is, when I print to the console I can see the data being passed clearly.
Uncaught TypeError: convos is undefined
function1
function fetchConversation(userID){
//loop through
usersms.forEach(function (sms,counter) {
//get userid from iteration
var userid_loop = sms.details[0].user.id;
//only display convo from specific user
if(userid_loop === userID){
//get all messages from this one user
var conversation = sms.details;
//transfer conversation to next function to display
showConversation(conversation);
//
}
//
});
}
function2
function showConversation(myconvo){
var convos = myconvo;
//iterate and append conversion
convos.forEach(function (msg,counter) {
console.log(msg.message);//prints all messages in the log
});
}
showConversation()//Uncaught TypeError: convos is undefined
Upvotes: 0
Views: 1264
Reputation: 1187
Your first error is that you are not passing in any arguments to the function.
Your second error is that msg.message doesn't exist - it's just msg by itself.
Also, the counter is unnecessary in this case.
function showConversation(myconvo) {
var convos = myconvo;
//iterate and append conversion
convos.forEach(function(msg) {
console.log(msg); //prints all messages in the log
});
}
showConversation(["element1", "element2"])
You also have an error here:
function fetchConversation(userID){
//loop through
usersms.forEach(function (sms,counter) {
//get userid from iteration
var userid_loop = sms.details[0].user.id;
//only display convo from specific user
if(userid_loop === userID){
//get all messages from this one user
var conversation = sms.details; //actually gets the details of the element it is iterating through, not all of them
//transfer conversation to next function to display
showConversation(conversation); //sends the details one by one for each iteration
//
}
//
});
}
Fix:
function fetchConversation(userID){
var conversation=[]
//loop through
usersms.forEach(function (sms,counter) {
//get userid from iteration
var userid_loop = sms.details[0].user.id;
//only display convo from specific user
if(userid_loop === userID){
//get all messages from this one user
conversation.push(sms.details);
}
});
//transfer conversation to next function to display
showConversation(conversation);
}
Upvotes: 0
Reputation: 142
I believe that you need to enter something within the brackets of showConversation().
You are assigning convos to myconvo, but myconvo doesn't exist because you didn't enter it as a parameter (value in the brackets).
Upvotes: 1