BookshelfJS A valid target model must be defined for the hasOne relation

421
February 25, 2017, at 04:23 AM

I am creating a profile system for a social networking site. I'm having trouble using relations to display various info about a user from different tables. Here are two of my BookshelfJS models:

Room:

var user    = require(__base + '/app/database/models/user/user').model;
exports.model = Application.Adapter.Model.extend({
    tableName: 'rooms',
    hasTimestamps: false,
    owner: function() { return this.hasOne(user, 'id', 'owner_id'); }
});

User:

var user_badges     = require(__base + '/app/database/models/user/user_badges').model,
    user_settings   = require(__base + '/app/database/models/user/user_settings').model,
    rooms           = require(__base + '/app/database/models/room/room').model,
    groups          = require(__base + '/app/database/models/group/group').model;
exports.model = Application.Adapter.Model.extend({
    tableName: 'users',
    hasTimestamps: false,
    badges: function() { return this.hasMany(user_badges, 'user_id', 'id'); },
    settings: function() { return this.hasMany(user_settings, 'user_id', 'id'); },
    rooms: function() { return this.hasMany(rooms, 'owner_id', 'id'); },
    groups: function() { return this.hasMany(groups, 'user_id', 'id'); }
});

Both of these models are used completely separate from one another on different pages. I'm currently using all the relations in the users model on the profile page and those are working. I have a different page which will display all info about a room along with the owners information. Here's how I'm using the room model so far

var room    = require(__base + '/app/database/models/room/room').model;
var room_id = 1;
return new room({id: room_id}).fetch({
    withRelated: [
        {'owner': function(qb) {
            qb.column('id','username');
        }}
    ],
    columns: ['id', 'owner_id', 'name']
})
.then(function resolve(result)
{
    if(result == null) return Promise.reject(new Error('invalid_room'));
    return Promise.resolve(result.toJSON());
},
function reject(err)
{
    return Promise.reject(err);
});

This is the error I'm receiving:

A valid target model must be defined for the rooms hasOne relation

I've tried using both hasOne & belongsTo but can't seem to get it working, if anyone has a solution please let me know thank you!

Rent Charter Buses Company
READ ALSO
How to convert the json from the webrequest in to a model in node.js/loopback?

How to convert the json from the webrequest in to a model in node.js/loopback?

I have a nested json object model like belowI want to use the REST Services exposed by iTunes to populate the below model

282
layout=column adding unnecessary padding

layout=column adding unnecessary padding

I am trying to center a container inside a CardViewIn order to do using Angular Material, I am making use of layout=column and layout-align=center center

293