我下面的函数,我比较两个相等的值(如显示在控制台,但回报是假的......ES6。如何正确比较===运算符?
function removeGroup(req, res, next) {
const user = req.user;
const found = user.groups.some((obj, idx) => {
console.log('obj._id: ', obj._id);
console.log('req.params.groupId: ', req.params.groupId);
if (obj._id === req.params.groupId) {
console.log('equal');
user.groups.splice(idx, 1);
return true;
}
console.log('not equal');
return false;
});
if (found) {
user.save()
.then(savedTable => res.json(savedTable))
.catch(e => next(e));
} else {
res.status(404);
res.json({ message: 'Group Not Found' });
}
}
这里是.LOG导致控制台
obj._id: 59109bc44ea63331151b9327
req.params.groupId: 59109bc44ea63331151b9327
not equal
显然,他们是不相等的。检查他们的类型(使用'typeof')。 – Bergi
@Bergi说了什么,'=='和'==='之间的区别在于它们必须是相同的类型。如果'=='返回false,那么还有其他事情正在进行。 –
[比较mongoose _id和字符串](http://stackoverflow.com/q/11637353/1048572)可能的重复?不幸的是,你没有提供任何有关这些对象的信息。 – Bergi