kontent-ai / delivery-sdk-js

Kontent Delivery SDK for Javascript
https://kontent.ai
MIT License
50 stars 34 forks source link

Unclear error message while not providing modular content data #40

Closed Simply007 closed 6 years ago

Simply007 commented 6 years ago

When I use the code from javascript penultimate code snippets from documentation: https://developer.kenticocloud.com/docs/get-titles-and-images-of-5-latest-articles

const KenticoCloud = require('kentico-cloud-delivery-typescript-sdk');
class Article extends KenticoCloud.ContentItem {
    constructor() {
        super();
    }
}
const config = new KenticoCloud.DeliveryClientConfig('975bf280-fd91-488c-994c-2f04416e5ee3', 
    [ new KenticoCloud.TypeResolver('article', () => new Article()) 
]);
const deliveryClient = new KenticoCloud.DeliveryClient(config);
deliveryClient.items()
    .type('article')
    .orderParameter('elements.post_date', KenticoCloud.SortOrder.desc)
    .limitParameter(5)
    .get()
    .subscribe(response => console.log(response));

I got en error (thce the same):

dist/services/query.service.js:49 Error: Cannot map fields because item is not defined
    at FieldMapService.mapFields (dist/services/field-map.service.js:32)
    at dist/services/field-map.service.js:117
    at Array.forEach (<anonymous>)
    at FieldMapService.mapRichTextField (dist/services/field-map.service.js:115)
    at FieldMapService.mapField (dist/services/field-map.service.js:93)
    at dist/services/field-map.service.js:69
    at Array.forEach (<anonymous>)
    at FieldMapService.mapFields (dist/services/field-map.service.js:54)
    at dist/services/field-map.service.js:182
    at Array.forEach (<anonymous>)

It is because loaded article is using modular content in the field body_copy and the data for modular content are not in the returned data (default value of the depth parameter is 1). Error is not clear about what really happens - I was force to debug the sdk to find the real cause. I am pretty OK with the error (maybe warning), but the message must me more specific.

Are we OK, that the code snippet in documentations leads to error, or we should add i.e. .depthParameter(2) to the query specification to prevent the error?

Simply007 commented 6 years ago

HINT: you can use http://requirebin.com/ for reproducing the issue - just paste the code open the console and run the code by the button.

JanLenoch commented 6 years ago

Well spotted! Let's raise the depth parameter then.

Enngage commented 6 years ago

Fixed, thanks for the detailed info!

Enngage commented 6 years ago

The error message will now contain further information to help users debug the issue in this format:

Modular content item with codename '${codename}' is not present in Delivery response. This modular item was requested by '${field.name}' field. Error can usually be solved by increasing 'Depth' parameter of your query.

Simply007 commented 6 years ago

Do we wan to add DepthParameter to the pre-ultimate code snippet on https://developer.kenticocloud.com/docs/get-titles-and-images-of-5-latest-articles to prevent raising error in case reader would try it? Or are we OK thet this code snippet is just a sample and just the ultimate one should be working on our sample project that is specifying fields, that does not require depth parameter to load the content?

JanLenoch commented 6 years ago

Corrected in the docu.