I don't get any errors, but I also don't get any references. If I change the global arrays to being fields of Course and Chapter, then bulk import reports the stack goes too deep which I'm assuming is the circular reference caused by double linking the related objects.
Have you referred to the Velo forum for a review and solution for your code yet? I seldom have had the need to use Velo when working with databases, I just simply use the multi-reference field to populate lists with a collection of information from various databases.
https://www.wix.com/velo/forum
What are you trying to do exactly? Usually, when I want to cross-reference one dataset with another, I make sure both datasets are on the page, that way the elements can draw from both.
Here's the data model.
The classes are defined like this
import wixData from 'wix-data'; let _chapters = [] let _lessons = [] class Group { constructor(key, arr) { this.key = key; this.array = arr; } } function groupBy(lessonsArr, property) { var reduced = lessonsArr.reduce((arr, obj) => { const key = obj[property]; if (!arr[key]) { arr[key] = []; } // Add object to list for given key's value arr[key].push(obj); return arr; }, {}); const groups = []; for(const item in reduced){ groups.push(new Group(item, reduced[item])) } return groups; } const uuid = require('uuid'); export class Course { constructor(course) { if(!course) { debugger; } let id = uuid.v4().toString(); this._id = id; this.slug = course.slug; const groups = groupBy(course.Lessons, "chapterTitle"); _chapters=_chapters.concat(groups.map(group => new Chapter(this, group))); } static fromArray(courses) { const result = []; for (const c of courses) { if (c.slug && c.Lessons) { result.push(new Course(c)); } } return result; } } export class Chapter { constructor(course, group) { if(!group || !group.array[0]) { debugger; } let id = uuid.v4().toString(); this._id = id; this["Course-2"] = course; const first = group.array[0]; this.chapterTitle = first.chapterTitle; _lessons=_lessons.concat(group.array.map(lesson => new Lesson(this, lesson))); } } export class Lesson { constructor(chapter, lesson) { if(!lesson) { debugger; } let id = uuid.v4().toString(); this._id = id; this.chapter = chapter; this.lessonTitle = lesson.lessonTitle; this.lessonUrl = lesson.lessonUrl; } } And it's imported with this:
async function importData() { try { await wixData.truncate("Course"); await wixData.truncate("Chapter"); await wixData.truncate("Lesson"); const array = set; // is parsed JSON const courses = Course.fromArray(array); return [ await wixData.bulkInsert("Course", courses), await wixData.bulkInsert("Chapter", _chapters), await wixData.bulkInsert("Lesson", _lessons) ] } catch (er) { debugger console.error(er) } finally{ debugger } }
I don't get any errors, but I also don't get any references. If I change the global arrays to being fields of Course and Chapter, then bulk import reports the stack goes too deep which I'm assuming is the circular reference caused by double linking the related objects.
What are you trying to do exactly? Usually, when I want to cross-reference one dataset with another, I make sure both datasets are on the page, that way the elements can draw from both.