gpt4 book ai didi

javascript - NodeJs 未定义不是一个函数

转载 作者:行者123 更新时间:2023-12-03 08:46:06 25 4
gpt4 key购买 nike

我正在使用 NodeJs 来运行此代码这是我的自定义模块

call = {};
call.hangup = {
searching: function(number, mysql, validator){
this.number = number;
this.mysql = mysql;
this.validator = validator;
var query = "{sql...}";
try
{
mysql.query(query, function(err, rows, fields) {
if (err) throw err;
if(!validator.isNull(rows))
{
return rows.leadid;
}else {
return false;
}
});
}catch(error)
{
console.log(error);
}
},
test: function(number, mysql, validator){
var self = this;
this.number = number;
this.mysql = mysql;
this.validator = validator;
var result = self.searching(number, mysql, validator);
console.log(result);
}
};
module.exports = call;

然后在我的主文件中调用测试函数

call.hangup.test(number, connection, validator);

但我在控制台中收到此错误代码:

                var result = self.searching(leadid, mysql, validator);
^
TypeError: undefined is not a function

我该如何修复它?为什么会发生这种情况?

最佳答案

您的this引用(您分配给自变量)指向测试函数,并且测试函数没有名为的函数>搜索。这就是您收到此错误的原因。您应该以这种方式调用 searching 函数 call.hangup.searching

call = {};
call.hangup = {
searching: function(number, mysql, validator){
this.number = number;
this.mysql = mysql;
this.validator = validator;
var query = "{sql...}";
try
{
mysql.query(query, function(err, rows, fields) {
if (err) throw err;
if(!validator.isNull(rows))
{
return rows.leadid;
}else {
return false;
}
});
}catch(error)
{
console.log(error);
}
},

test: function(number, mysql, validator){

this.number = number;
this.mysql = mysql;
this.validator = validator;
var result = call.hangup.searching(number, mysql, validator);
console.log(result);
}
};
module.exports = call;

编辑:您还可以将 self 变量分配给 call.hangup

test: function(number, mysql, validator){
var self = call.hangup;
this.number = number;
this.mysql = mysql;
this.validator = validator;
var result = self.searching(number, mysql, validator);
console.log(result);
}

关于javascript - NodeJs 未定义不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32881177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com