You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

类Tinder交友应用JS推荐校验函数返回null问题排查

问题1:核心校验函数逻辑缺失返回语句

你写的hydratedValidRecommendation函数,进入第一层条件判断后,仅在!location || !myLocation的场景下返回了otherUser,其余场景(即双方位置都存在、计算完距离之后)没有任何返回逻辑,会直接跳出条件判断执行最后的return null。
同时你计算的isWithinDistanceRadius变量完全没有被使用,缺少距离符合要求后的返回逻辑。

问题2:过滤函数回调没有返回值

filteredAndHydratedRecommendations中的filter方法回调函数没有写return,执行后永远返回undefined,会把所有合法的推荐结果都过滤掉,最终返回空数组。
原错误代码:

return hydratedRecommendations.filter((recommendation) => {
  recommendation != null;
});

问题3:数组和字符串直接比较导致条件不成立

第一层判断条件中的appDistance == "100000"永远为假:appDistance是split后返回的数组(默认值是["100000"]),数组和字符串做相等判断时不会自动取第一个元素比较,导致条件触发失败。


修复方案

第一步:修正hydratedValidRecommendation的返回逻辑

在计算完isWithinDistanceRadius后新增判断,符合距离要求就返回用户,同时修正第一层判断中的距离比较逻辑:

// 原第一层判断里的(appDistance == "100000")修改为(distanceValue == 100000)

// 计算完距离后新增判断
const otherUserDistanceNumber = otherUser.distance.match(/\d+/);
const isWithinDistanceRadius =
  otherUserDistanceNumber?.length > 0 &&
  Number(otherUserDistanceNumber[0]) <= distanceValue;
// 新增距离校验通过后的返回
if (isWithinDistanceRadius) {
  return otherUser;
}

第二步:修正过滤函数的返回

修改filter回调,加上return:

// 写法1
return hydratedRecommendations.filter((recommendation) => {
  return recommendation != null;
});
// 写法2(更简洁)
return hydratedRecommendations.filter(recommendation => recommendation != null);

内容的提问来源于stack exchange,提问作者Arnaud P

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 11:57:00