0

所以我正在使用 socket.io 开发一个聊天应用程序,我想确定它是我的消息还是其他人的消息。我为此使用的两件事是我从本地存储中检索的全名和当我发送新消息时从服务器返回的全名。有人知道为什么即使它们都是字符串并且它们都由相同的单词组成,为什么它们仍然不一样吗?

isItMyMsg(message){
        console.log(localStorage.getItem('fullName'));
        console.log(message.fullname);
        console.log(localStorage.getItem('fullName') === message.fullName);
        return localStorage.getItem('fullName') === message.fullName
    }

这是我拍摄的记录结果的屏幕截图。两个是本地存储和服务器对象的结果,错误是我评估它们是否相同时的结果。我期待它返回 TRUE,但我得到了 FALSE

在此处输入图像描述

4

1 回答 1

6

试试这个:1-检查是否可能有多余的空格,所以这样做:

const first = localStorage.getItem('fullName').trim();
const second = message.fullName.trim();

然后做:

console.log(first === second);

如果这不起作用,请尝试:

const first = localStorage.getItem('fullName').trim().toLowerCase();
const second = message.fullname.trim().toLowerCase();

看看区分大小写是否有任何区别。

于 2018-05-18T17:37:09.867 回答