[英]checking undefined or null in javascript
I have below method in nestjs
.我在
nestjs
中有以下方法。
async findOne(userId: string) {
const user = await this.userRepository.find({userId});
if (user) {
throw new NotFoundException(`User does not exist`);
}
return user;
}
this method retunrn an array of object
.此方法返回 object 的
array of object
。 if value not found in DB it return [{}]
.如果在数据库中找不到值,则返回
[{}]
。 I need to throw exception when it return empty array
I mean [{}]
.当它返回
empty array
时我需要抛出异常我的意思是[{}]
。 for that I need to put condition
in if
block when I write user
, or user==null
or user.length==o
in if
block, in all the case it is not going inside the if
为此,我需要在编写
user
时将condition
放入if
块中,或者在if
块中放入user==null
或user.length==o
,在所有情况下它都不会进入if
what modification I need to do in if
block?我需要在
if
块中做哪些修改?
It looks like you need to check if user
is an array with length = 1 where the first element is an empty object. You can do something like this:看起来您需要检查
user
是否是长度为 1 的数组,其中第一个元素为空 object。您可以这样做:
const user = await this.userRepository.find({ userId });
if (
!user ||
user.length === 0 ||
(user.length === 1 && Object.keys(user[0]).length === 0)
) {
throw new NotFoundException(`User does not exist`);
}
return user;
You will need to look into your userRepositoy.find()
function to see if there are any other possible values it could return.您将需要查看您的
userRepositoy.find()
function 以查看它是否可以返回任何其他可能的值。 From your description, it either returns an array of users or an array with a single empty object. However, there may be other failure cases where it returns something different.根据您的描述,它要么返回一个用户数组,要么返回一个包含单个空 object 的数组。但是,在其他失败情况下,它可能会返回不同的内容。 That part is up to you to make sure you handle those cases properly.
这部分取决于您,以确保您正确处理这些案件。
I was wondering why you actually need to use find
method.我想知道为什么您实际上需要使用
find
方法。
You are trying to get a user based on userId
in usersRepository, isn't userId supposed to be unique?您正在尝试根据 usersRepository 中的
userId
获取用户,userId 不应该是唯一的吗?
If it's unique then you are getting either an empty array as you described or with only one object in it.如果它是唯一的,那么您将得到一个如您所描述的空数组,或者其中只有一个 object。 Not sure what DB you are using but in any case,
findOne
method should be there.不确定您使用的是什么数据库,但无论如何,
findOne
方法应该在那里。
It this applies to your case findOne
should do the trick这适用于您的情况
findOne
应该可以解决问题
const user = await this.userRepository.findOne({ userId });
if (!user) throw new NotFoundException(`User does not exist`);
return user;
Or if you have to use find
and you want to make if statement more readable I suggest using Ramda , lodash or any similar library to keep it cleaner.或者,如果你必须使用
find
并且你想让 if 语句更具可读性,我建议使用Ramda 、 lodash或任何类似的库来保持它的清洁。
With Ramda you could do as following使用 Ramda,您可以执行以下操作
const [firstUser = {}, ...others] = await this.userRepository.find({ userId });
if(R.isEmpty(firstUser)) throw new NotFoundException(`User does not exist`);
return [firstUser, ...others];
I highly assume that fetched data based on userId
is just one record so I believe you can use just like this我高度假设基于
userId
获取的数据只是一条记录,所以我相信你可以像这样使用
const [user = {}] = await this.userRepository.find({ userId });
if(R.isEmpty(user)) throw new NotFoundException(`User does not exist`);
return user;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.