Can't access field from database query

I was wondering if anyone could shed some light on why I can’t access the ‘courseCategory’ in my database? I need it to be able to use wixLocation but when I use console.log is shows ‘undefined’ every other element works from the code but this has stumped me… spelling is 100% correct.

import { getCourses } from 'backend/queries';
import wixLocation from 'wix-location';

$w.onReady(function () {
    $w('#courseImage').src = null; // ignore error, trick to prevent placeholder picture from displaying
    $w("#repeater1").onItemReady(($w, itemData, index) => {
 let coursesCategory = itemData.coursesCategory;
        console.log(coursesCategory)
        console.log(itemData.title)
        $w("#courseTitle").text = itemData.title;
        $w('#courseImage').src = itemData.image;
        $w('#duration').text = itemData.duration;
        $w('#price').text = "From " + itemData.individaulPrice;
        $w('#grade').text = itemData.lightbox;
        $w('#location').text = itemData.prices;
        $w("#description").text = itemData.description;
 let test = $w("#description").text;
 let shortenedtext = test.slice(0, 230) + "...";
 if (test.length > 100) {
            $w("#description").text = shortenedtext;
        }
 let avgRating = (Math.round(itemData.rating * 10 / itemData.count) / 10);
        $w('#ratingsDisplay').rating = avgRating;
        $w('#ratingsDisplay').numRatings = itemData.count;
        $w('#courseImage').onClick((event) => {
            console.log(coursesCategory)
 //$w('#loadingLogo').show();
 //wixLocation.to("/courses/" + encodeURI(coursesCategory) + "/" + encodeURI(itemData.title));
        })
        $w('#courseTitle').onClick((event) => { 
            $w('#loadingLogo').show();      
            wixLocation.to("/courses/" + encodeURI(itemData.coursesCategory) + "/" + encodeURI(itemData.title));
        })
        $w('#datesButton').onClick((event) => { 
            $w('#loadingLogo').show();
            $w('#datesButton').label = "Please Wait...";
            wixLocation.to("/courses/" + encodeURI(itemData.coursesCategory) + "/" + encodeURI(itemData.title) + "#anchor1");
        })
        $w('#courseInfoButton').onClick((event) => {
            $w('#loadingLogo').show();
            $w('#courseInfoButton').label = "Please Wait...";
            wixLocation.to("/courses/" + encodeURI(itemData.coursesCategory) + "/" + encodeURI(itemData.title));
        })
        console.log(itemData.lightbox)
    });

    getCourses("all").then(function (resp) {
 let items = resp.items;
 let courses = [];
        items.forEach(function (item) {
 let course = { "rating": item.rating, "_id": item._id, "title": item.title, "image": item.image, "duration": item.duration, "individaulPrice": item.individaulPrice, "lightbox": item.lightbox, "prices": item.prices, "description": item.description, "count": item.count };
            courses.push(course);
            console.log(course)
        });

        $w("#repeater1").data = []; 
        $w("#repeater1").data = courses; 
        
    });
});

Thank you!

In the question you wrote courseCategory, in your code you wrote course s Category . So which is right and which is wrong?

Hi JD, the actual code has the correct spelling, coursesCategory

So the problem is not here. Maybe you only have this field in the collection sandbox? Try console.log(itemData) and see if this field is there or not.

@jonatandor35 Got it, thanks for the tip. I needed to add some text to this line of code.

let course = {"coursesCategory": item.coursesCategory, "rating": item.rating, "_id": item._id, "title": item.title, "image": item.image, "duration": item.duration, "individaulPrice": item.individaulPrice, "lightbox": item.lightbox, "prices": item.prices, "description": item.description, "count": item.count };

Thanks again :slight_smile: