我正在开发节点js,并通过URL查询数据。
get_posts_default?pageId=ge4JqBn9F0srzHnVFHmh&asking_post=false&asking_responce=false&maxSort=-1&minSort=-1&limit=20这是负责处理此请求的函数。
public async get_poset_list(userDeta: hs_I_fb_en_user_auth_paylode,pageId:string,asking_post:boolean,asking_responce:boolean,maxSort:number,minSort:number,limit:number):Promise<hs_I_fb_en_post_return[]>{
try {
hs_d_w("Is asking post: - "+asking_post);
hs_d_w("Limit: - "+limit);
if(asking_post===true){
hs_d_w("Asking post true");
if(minSort<=-1 && maxSort<=-1){
hs_d_w("Asking post Defolt");
return this._postQueryes.get_only_poses(pageId,limit);
}else{
if(minSort>-1){
hs_d_w("Asking post MIn");
return this._postQueryes.get_only_poses_min(pageId,minSort,limit);
}
if(maxSort>-1){
hs_d_w("Asking post Max");
return this._postQueryes.get_only_poses_max(pageId,maxSort,limit);
}
hs_d_w("Asking post None");
return [];
}
}else{
if(minSort<=-1 && maxSort<=-1){
hs_d_w("Asking talk Defolt");
return this._postQueryes.get_only_talkes(pageId,limit);
}else{
if(minSort>-1){
hs_d_w("Asking talk min");
return this._postQueryes.get_only_talkes_min(pageId,minSort,limit);
}
if(maxSort>-1){
hs_d_w("Asking talk max");
return this._postQueryes.get_only_talkes_max(pageId,maxSort,limit);
}
hs_d_w("Asking talk none");
return [];
}
}
} catch (e) {
hs_d_w("get_poset_list : " + e);
return Promise.reject(e)
}
}现在,如果我调用set asking_post=false或asking_post=true,它总是调用该函数的主要其他区域
return this._postQueryes.get_only_talkes(pageId,limit);这一个。
我不明白为什么会发生这种事?有人能帮我吗?
发布于 2018-08-07 06:02:48
当您从req.query中得到一些东西时,它总是返回一个String。因此,请确保将其转换为布尔值
const variable = (variable == 'true')
// or
const variable = (variable === 'true')另外,当变量是布尔变量时,您不必使用===显式检查。这也能起作用
if(foo) {
} else {
}编辑:正如@Kamalakannan所说,Boolean('string')将无法工作。我很抱歉。
发布于 2018-08-07 06:08:11
查询参数被认为是strings。因此,如果你与===检查,这将是虚假的。
进行字符串比较,如if ("true" === asking_post)或if ("false" === asking_post)
布尔值(Asking_post)总是返回字符串值的true。
const t = Boolean("true");
const f = Boolean("false");
console.log("Value of 'true':", t);
console.log("Value of 'false':", f);
所以不要使用Boolean(asking_post)。
发布于 2018-08-07 17:58:04
您可以简单地使用JSON.parse来转换它。
const x = JSON.parse('true');
const y = JSON.parse('false');它将返回两者的布尔值。
https://stackoverflow.com/questions/51719785
复制相似问题