如何防止为实体增加价值或将其推入items
数组?
我尝试从undefined
返回idAttribute
,但是仍然将undefined
推入items
数组中,并且还在其中添加了一个名为undefined
的键。实体。
const getGuid = (entity: any) => {
if (entity.enclosure) {
if (entity.guid) {
return entity.guid
} else if (entity.enclosure.url) {
return entity.enclosure.url
}
}
}
const fetchPodcastRssEpisodeSchema = new schema.Entity(
'episodes',
{},
{
idAttribute: getGuid,
processStrategy: (entity, parent, _key) => {
const itunes = entity.itunes || {}
const enclosure = entity.enclosure || {}
const image =
itunes.image ||
(parent.image && parent.image.url) ||
(parent.itunes && parent.itunes.image)
return {
artist: parent.title,
content: entity.content,
durationSeconds: utils.transformDurationToSecs(itunes.duration),
guid: getGuid(entity),
image,
pubDate: entity.isoDate,
title: entity.title,
url: enclosure.url,
}
},
}
)
仅当该项目从idAttribute
返回有效值时,才应添加该项目。有什么办法可以做到这一点?
谢谢
答案 0 :(得分:0)
更新
const getGuid = (entity: any) => {
if (entity && entity.enclosure) {
if (entity && entity.guid) {
return entity.guid
} else if (entity && entity.enclosure && entity.enclosure.url) {
return entity.enclosure.url
}
}
}