首页 > 解决方案 > Normalizr :如果 id 存在,则仅将项目添加到项目数组

问题描述

如何防止向实体添加值或将其推送到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. 有没有办法可以做到这一点?

谢谢

标签: reactjsreduxreact-reduxnormalizr

解决方案


更新

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

推荐阅读