我在Service.ts文件的typecrypt中有一个功能:
export const doCallAuth = (username, password) => {
var auth = new Auth({
url: '...',
});
var status;
auth.authenticate(username, password, function (err, user) {
if (err) {
console.log(err);
status = 'no';
} else if (!user.uid) {
console.log("user not found Error");
status = 'no';
} else if (user.uid) {
console.log("success : user " + user.uid + " found ");
status = 'yes';
}
});
return status;
}
我通过以下方式调用此方法:
var result = Service.doCallAuth('test', 'test');
并且变量结果未定义
我不知道为什么结果未定义
有人可以帮我吗?
谢谢。 :)
尝试这个。
auth.authenticate(username, password, function (err, user) {
if (err) {
console.log(err);
status = 'no';
} else if (!user.uid) {
console.log("user not found Error");
status = 'no';
} else if (user.uid) {
console.log("success : user " + user.uid + " found ");
status = 'yes';
}
return status
});
用户async
和await
获得值
export const doCallAuth = async function(username, password) => {
var auth = new Auth({
url: '...',
});
var status;
auth.authenticate(username, password, function (err, user) {
if (err) {
console.log(err);
status = 'no';
} else if (!user.uid) {
console.log("user not found Error");
status = 'no';
} else if (user.uid) {
console.log("success : user " + user.uid + " found ");
status = 'yes';
}
});
return status;
}
通过以下方式调用此方法:
var result = await Service.doCallAuth('test', 'test');