gpt4 book ai didi

javascript - 类型错误 : Cannot call method 'toLowerCase' of undefined

转载 作者:搜寻专家 更新时间:2023-10-31 23:33:15 25 4
gpt4 key购买 nike

我遇到了一个错误,我不知道为什么。我尝试将所有字符转换为小写,然后再插入数据库并进行比较。在第一个函数 this.addUser 我调用 toLower 将其转换为小写它没有问题但是在第二个函数 this.checkPublicUsername 我调用 toLower 它在我的终端中显示错误说 TypeError: Cannot call method 'toLowerCase' of undefined

function toLower (text) {
return text.toLowerCase();
}

this.addUser = function(data,callback) {
"use strict";
// Generate password hash
var salt = bcrypt.genSaltSync();
var password_hash = bcrypt.hashSync(data.password, salt);
// transform all character into lowercase
var real = toLower(data.publicUsername);
// Create user document
var user = {
'_id': data.email,
'email':data.email,
'password': password_hash,
'name':{
'firstName': data.firstName,
'lastName': data.lastName
},
'penName': data.penName,
'publicUsername':{
'display':data.publicUsername,
'real':real
}
};
users.insert(user, function (err, result) {
"use strict";

if (!err) {
console.log("Inserted new user");
return callback(null, result[0]);
}
if (err) console.log(err)

return callback(err, null);
});
}

this.checkPublicUsername = function(data,callback) {
"use strict";
var real = toLower(data);
var publicUsername = {
"publicUsername.real":real
}
users.find(publicUsername).count(function(err, result) {
callback(null,result);
})

}

调用 checkPublicUsername

 users.checkPublicUsername(data.publicUsername, function(err, result){
if(result){
callback(new Error('"'+data.publicUsername+'" has been taken. Please choose another.'), null);
}
});

最佳答案

您收到此错误是因为 data.publicUsername 未定义,请检查您传递给此函数的值。

关于javascript - 类型错误 : Cannot call method 'toLowerCase' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20371623/

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