Contentful logo

Contentful Community

Error fetching content from Contentful - Contentful.SDKError error 2

The following function throws an error:

public func fetchStories() {
		client.fetchArray(of: CFStory.self, matching:  QueryOn<CFStory>.include(10)) { (result: Result<HomogeneousArrayResponse<CFStory>>) in
			switch result {
			case .success(let result):
				for story in result.items {
					self.stories[story.id] = story
				}
			case .error(let error):
				print("Error fetching stories: \(error.localizedDescription)")
			}
			self.fetchNextRootResource()
		}
	}

The error message is:

[Contentful] Error: keyNotFound(FieldKeys(stringValue: "text", intValue: nil), Swift.DecodingError.Context(codingPath: [ArrayCodingKeys(stringValue: "includes", intValue: nil), _JSONKey(stringValue: "Index 118", intValue: 118)], debugDescription: "No value associated with key FieldKeys(stringValue: \"text\", intValue: nil) (\"text\").", underlyingError: nil))
Error fetching stories: The operation couldn’t be completed. (Contentful.SDKError error 2.)
(lldb) 

And here is the code that deals with the fieldkeys belonging the the class CFstory

import Contentful

final class CFStory: EntryDecodable, FieldKeysQueryable, Equatable, Resource {

	static func == (lhs: CFStory, rhs: CFStory) -> Bool {
		lhs.sys.id == rhs.sys.id
	}
	
	enum FieldKeys: String, CodingKey {
		case title
		case introduction
		case conclusion
		case portals = "cpPortals"
		case storyColor
	}

	static let contentTypeId: String = "cpStory"

	public let sys: Sys
	public let title: String?
	public var introduction: NarrativeContent?
	public var conclusion: NarrativeContent?
	public var portals: [CFPortal]?
	public let colorString: String?

	public required init(from decoder: Decoder) throws {
		sys = try decoder.sys()

		let fields = try decoder.contentfulFieldsContainer(keyedBy: CFStory.FieldKeys.self)

		title = try fields.decodeIfPresent(String.self, forKey: .title)
		colorString = try fields.decodeIfPresent(String.self, forKey: .storyColor)

		try fields.resolveLink(forKey: .introduction, decoder: decoder) { [weak self] introduction in
			self?.introduction = introduction as? NarrativeContent
		}
		try fields.resolveLink(forKey: .conclusion, decoder: decoder) { [weak self] conclusion in
			self?.conclusion = conclusion as? NarrativeContent
		}
		try fields.resolveLinksArray(forKey: .portals, decoder: decoder) { [weak self] portals in
			self?.portals = portals as? [CFPortal]
		}
	}
}

extension CFStory {
	enum StoryColor: String {
		case yellow, pink, red, brown, ochre, cyan
		case champagne
	}

	var color: UIColor {
		return UIColor.storyColor[StoryColor(rawValue: colorString ?? "champagne") ?? .champagne] ?? UIColor.champagneBranded
	}
}

Any suggestion why I get this error?