0

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
8
  • Try doing showConversation(myconvo) instead of showConversation() Commented Jan 10, 2021 at 1:58
  • @Endothermic_Dragon: yes I did try that. It would have been an easy fix. I wouldn't be here. Commented Jan 10, 2021 at 2:00
  • Why are you calling showConversation() on its own? Commented Jan 10, 2021 at 2:07
  • @Nick: display result in html from the index page. I do this with a different function and it works. This one is undefined Commented Jan 10, 2021 at 2:14
  • Well just to be totally clear what your error is: if you do showConversation(), you have passed no arguments, meaning myconvo is undefined. Then you try to do undefined.forEach, which will explode because it's not an array. Commented Jan 10, 2021 at 2:18

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

1 Comment

What would that variable be? I tried with different variables and get undefined
0

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);
}

2 Comments

What would be those elements?
That depends on what you are trying to do. I just fixed it so it works. It looks like you are trying to pass through snippets of a chat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.