如果数组为空,return 如何在节点中使用 false 值承诺方法,反之亦然
How can return false value promise method in node if array is empty and vise versa
我正在尝试 promise 代码,但它总是 return 我解决了,即使数据库中不存在该用户
谁能帮我修复我的代码和 return 声明
在 return 函数中,第二个控制台日志仅在工作。
这是我的代码
Api 调用
const email = 't@t.com';
const request = require('request');
function IsUserExists(email, kc_accessToken) {
let url = `${path}/users?email=${email}`;
return new Promise(function (resolve, reject) {
request(
{
url: url,
headers: {
'content-type': 'application/json',
authorization: `Bearer ${kc_accessToken}`,
},
},
function (error, response, body) {
if (error) {
console.log('some error occured');
}
if (response.body.length > 0) {
console.log('User Exist');
return resolve();
}
console.log('Does not Exist');
return reject();
}
);
});
}
函数调用
http
.createServer(function Test() {
getAccessToken()
.then(function (response) {
kc_accessToken = response.data.access_token;
IsUserExists(email, kc_accessToken).then((resp) => {
if (resp) {
console.log('Do Not Create');
} else if (!resp) {
console.log('Creat a new User');
}
});
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
})
.listen(8081);
当提供存在的用户电子邮件时 ( t@t.com )
当提供的用户邮箱不存在时(09@t.com)
我需要为您在评论中提出的问题创建一个新答案。
现在,您进入拒绝功能,因此您需要在外部处理此拒绝。
if (response.body.length > 0) {
console.log('User Exist');
return resolve();
}
console.log('Does not Exist');
return reject(); // -> Now here you are
需要在IsUserExists.then()
后添加.catch
功能。
它将是 IsUserExists.then().catch()
http.createServer(function Test() {
getAccessToken()
.then(function (response) {
kc_accessToken = response.data.access_token;
// here you only accept the data from resolve in Promise
// so you need to add .catch function to handle the rejection.
IsUserExists(email, kc_accessToken).then((resp) => {
if (resp) {
console.log('Do Not Create');
} else if (!resp) {
console.log('Creat a new User');
}
}).catch((error) => {
console.log(error)
});
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
})
.listen(8081);
顺便说一句,您可以在拒绝函数中添加参数,例如 reject(new Error("user not found))
。
那么在外面,就可以得到这个拒绝信息。
我正在尝试 promise 代码,但它总是 return 我解决了,即使数据库中不存在该用户 谁能帮我修复我的代码和 return 声明 在 return 函数中,第二个控制台日志仅在工作。
这是我的代码
Api 调用
const email = 't@t.com';
const request = require('request');
function IsUserExists(email, kc_accessToken) {
let url = `${path}/users?email=${email}`;
return new Promise(function (resolve, reject) {
request(
{
url: url,
headers: {
'content-type': 'application/json',
authorization: `Bearer ${kc_accessToken}`,
},
},
function (error, response, body) {
if (error) {
console.log('some error occured');
}
if (response.body.length > 0) {
console.log('User Exist');
return resolve();
}
console.log('Does not Exist');
return reject();
}
);
});
}
函数调用
http
.createServer(function Test() {
getAccessToken()
.then(function (response) {
kc_accessToken = response.data.access_token;
IsUserExists(email, kc_accessToken).then((resp) => {
if (resp) {
console.log('Do Not Create');
} else if (!resp) {
console.log('Creat a new User');
}
});
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
})
.listen(8081);
当提供存在的用户电子邮件时 ( t@t.com )
当提供的用户邮箱不存在时(09@t.com)
我需要为您在评论中提出的问题创建一个新答案。
现在,您进入拒绝功能,因此您需要在外部处理此拒绝。
if (response.body.length > 0) {
console.log('User Exist');
return resolve();
}
console.log('Does not Exist');
return reject(); // -> Now here you are
需要在IsUserExists.then()
后添加.catch
功能。
它将是 IsUserExists.then().catch()
http.createServer(function Test() {
getAccessToken()
.then(function (response) {
kc_accessToken = response.data.access_token;
// here you only accept the data from resolve in Promise
// so you need to add .catch function to handle the rejection.
IsUserExists(email, kc_accessToken).then((resp) => {
if (resp) {
console.log('Do Not Create');
} else if (!resp) {
console.log('Creat a new User');
}
}).catch((error) => {
console.log(error)
});
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
})
.listen(8081);
顺便说一句,您可以在拒绝函数中添加参数,例如 reject(new Error("user not found))
。
那么在外面,就可以得到这个拒绝信息。