简体   繁体   English

从另一个 function 调用 function 时获取未定义的变量

[英]Getting undefined variable when calling function from another function

Scratching my head.抓着我的头。 I'm getting an undefined error when trying to call a function.尝试调用 function 时出现未定义的错误。 Thing is, when I print to the console I can see the data being passed clearly.问题是,当我打印到控制台时,我可以清楚地看到正在传递的数据。

Uncaught TypeError: convos is undefined未捕获的类型错误:convos 未定义

function1功能1

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功能2

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

I believe that you need to enter something within the brackets of showConversation().我相信您需要在 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).您正在将 convos 分配给 myconvo,但 myconvo 不存在,因为您没有将其作为参数输入(括号中的值)。

Your first error is that you are not passing in any arguments to the function.您的第一个错误是您没有将任何 arguments 传递给 function。

Your second error is that msg.message doesn't exist - it's just msg by itself.您的第二个错误是 msg.message 不存在 - 它只是 msg 本身。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM