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.

Possibly unhandled Error: A valid target model must be defined for the workspaces belongsToManyrelation

See original GitHub issue

Hi!

I was testing out bookshelf but I am getting this error and I just don’t know how to fix it. Could you please help me out?

I got the following models:

var Bookshelf = require('bookshelf').DB;
var User = require("./user").model;

exports.model = Bookshelf.Model.extend(
{
    tableName: "workspaces",
    users: function() 
    {
        return this.belongsToMany(User, "userworkspaces", "WorkspaceId", "UserId");
    }
});

and

var Bookshelf = require('bookshelf').DB;
var Workspace = require("./workspace").model;

exports.model = Bookshelf.Model.extend(
{
    tableName: "users",
    hidden: ['password', 'activationToken', 'forgotToken', 'forgotDate'],   
    workspaces: function() 
    {
        return this.belongsToMany(Workspace, "userworkspaces", "UserId", "WorkspaceId");
    },
    virtuals: 
    {
        fullName: function() 
        {
            return this.get('firstName') + ' ' + this.get('lastName');
        }
    }
});

When I do the following it works just fine and as expected

new Users().fetch({
        withRelated: ['workspaces']
    }).then(function(collection) {
        res.send(collection.toJSON());
    });

but when I try:

    new Workspaces().fetch({
        withRelated: ['users']
    }).then(function(collection) {
        res.send(collection.toJSON());
    }); 

It gives me the following error:

Possibly unhandled Error: A valid target model must be defined for the workspaces belongsToManyrelation
    at ModelCtor.Model.SqlModel.extend._relation (C:\projects\bookshelf_test\src\node_modules\bookshelf\bookshelf.js:50:15)

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
danieldiekmeiercommented, Jan 3, 2017

For anyone coming from Google (like me), here is the current link to the Wiki for the Registry plugin: https://github.com/tgriesser/bookshelf/wiki/Plugin:-Model-Registry

1reaction
quintenbcommented, Mar 16, 2014

Never mind. I think I just found it. It had to do with the way I was loading the models. The syntax was correct.

Read more comments on GitHub >

github_iconTop Results From Across the Web

BookshelfJS A valid target model must be defined for the ...
In your case it looks like Application.Adapter is an instance of Bookshelf, so if you add the registry plugin to it (with Application.Adapter....
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