【发布时间】:2016-08-20 12:59:18
【问题描述】:
我正在构建一个使用 Kinvey mbaas 作为数据库的聊天应用程序。我有一个存储聊天的集合,其中包含以下数据列:_id、firstUser、otherUsers、history。到目前为止的想法是:当发布消息时,请求 GET 以查找两个用户之间是否存在聊天。 GET 获取整个集合并遍历条目并检查 firstUser 和 otherUsers 是否匹配。这就是问题所在:它们永远不匹配。代码如下:
for (let entity = 0; entity < response.length; entity++) {
console.log('DEV_firstUser: '
+ response[entity]['firstUser'] + '|' + firstUser);
console.log('DEV_otherUsers: |'
+ response[entity]['otherUsers'] + '|' + otherUsers + "|");
console.log(response[entity]['firstUser'] === firstUser);
console.log(response[entity]['otherUsers'] === otherUsers);
// The problematic condition - the logs above are demonstrations.
if (response[entity]['firstUser'] === firstUser
&& response[entity]['otherUsers'] === otherUsers) {
id = response[entity]['_id'];
console.log('DEV_id: ' + id);
index = entity;
console.log(response[index][id]);
}
}
'response' 是集合——我可以看到的对象数组。 “实体”很简单——集合中的每个实体。 'otherUsers' 是数组。
这是我在控制台上得到的:
DEV_firstUser: alex|alex
DEV_otherUsers:|Ganka|Ganka|
true
false
【问题讨论】:
-
请把它归结为几十行代码。
标签: javascript logical-operators kinvey