Normalizr:仅当存在ID时,才将项目添加到项目数组

时间:2019-07-15 07:35:08

标签: reactjs redux react-redux normalizr

如何防止为实体增加价值或将其推入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返回有效值时,才应添加该项目。有什么办法可以做到这一点?

谢谢

1 个答案:

答案 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
      }
    }
 }