question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Virtual list creates circular reference error when trying to include virtuals in query result

See original GitHub issue

Hey,

on a List register there’s a piece of code that adds ‘list’ as a virtual field.

this.schema.virtual('list').get(function () {
        return list;
    });

This creates some problems when you add your own virtuals and want them included in query result you’re going to parse to JSON (for an API endpoint for example).

Take this example:

var Waypoint = new keystone.List('Waypoint', {
    autokey: { path: 'slug', from: 'name', unique: true }
});

...


//Check if waypoint is available
Waypoint.schema.virtual('available').get(function() {
    console.log('Calling available getter');
    if(this.conditionsOn) {
        return availabilityUtils.check(this.conditions.days, this.conditions.start, this.conditions.end);
    } else {
        return true;
    }
});

...

Waypoint.schema.set('toJSON', {
    virtuals: true
});
Waypoint.addPattern('standard meta');
Waypoint.defaultColumns = 'title, type';
Waypoint.register();

In this case, querying a waypoint and returning it with res.apiResponse() calls the Object.stringify method but generates an TypeError: Converting circular structure to JSON error because of the list virtual.

Best practices or workarounds?

Kind regards, David

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ignlgcommented, Sep 11, 2014

Just in case that someone needs a quick & dirty fix for this, I’ve created a gist with a code that deletes every list inside the model prior to export it: https://gist.github.com/ignlg/519a4b6b9845d7442a48

Hope it helps meanwhile.

0reactions
Twansparantcommented, Apr 20, 2018

Thanks @ignlg for your gist, thos fixed my problem!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove or allow a circular reference - Microsoft Support
If you suspect you have a circular reference in a cell that isn't showing a zero, try this: Click the formula in the...
Read more >
How to fix a circular reference in Excel - Spreadsheet Class
This causes a circular reference error. To fix this error, simply add the tab name to the references in the filter formula. The...
Read more >
A circular reference was detected while serializing an object of ...
It seems that there are circular references in your object hierarchy which is not supported by the JSON serializer.
Read more >
Circular Reference Error in Excel and How to Fix it (+ video ...
A Circular Reference is a type of error in Excel that happens when a formula in a cell tries to calculate itself. Essentially...
Read more >
mongoose/index.d.ts at master - GitHub
// interface breaks on Mongoose Document class due to circular references. // Re-evaluate this when we drop `extends Document` support in document interfaces....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found