挠我的头。尝试调用函数时出现未定义错误。问题是,当我打印到控制台时,我可以清楚地看到正在传递的数据。
<块引用>未捕获的类型错误:convos 未定义
功能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);
//
}
//
});
}
功能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
答案 0 :(得分:1)
我认为您需要在 showConversation() 的括号内输入一些内容。
您正在为 myconvo 分配 convo,但 myconvo 不存在,因为您没有将其作为参数(括号中的值)输入。
答案 1 :(得分:0)
您的第一个错误是您没有向函数传递任何参数。
您的第二个错误是 msg.message 不存在 - 它只是 msg 本身。
此外,在这种情况下不需要计数器。
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"])
您在这里也有错误:
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
//
}
//
});
}
修复:
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);
}