Bookshelf: .fetchOne is not a function

Created on 7 Dec 2017  Â·  3Comments  Â·  Source: bookshelf/bookshelf

I'm following the docs to do the following:

var sessions = require('../models/sessions.js');
        var users = require('../models/users.js');

        sessions().where('key', sess_key).fetchOne({withRelated: ['users']}).then(function(row) {
            callback(false, row.related('users'));
        }).catch(function(err) {
            callback(err);
        });

But this is what I get:

TypeError: sessions(...).where(...).fetchOne is not a function

sessions.js is:

module.exports = function() {
    return global.db.bookshelf.Model.extend({
        tableName: 'sessions',
        hasTimestamps: true
    });
}
question

Most helpful comment

Right, the problem here is that fetchOne() is a collection method, not a model method, and you're trying to use it on a model (the return value of model.where()). You can achieve what you want with:

sessions().collection().where('key', sess_key).fetchOne({withRelated: ['users']}).then(function(row) {
  callback(false, row.related('users'));
}).catch(function(err) {
  callback(err);
});

All 3 comments

Right, the problem here is that fetchOne() is a collection method, not a model method, and you're trying to use it on a model (the return value of model.where()). You can achieve what you want with:

sessions().collection().where('key', sess_key).fetchOne({withRelated: ['users']}).then(function(row) {
  callback(false, row.related('users'));
}).catch(function(err) {
  callback(err);
});

Did you try sessions().where() ?

P.S.: I'd advise you to use bookshelf's registry or you may end up with
circular dependencies.

On Thu, Dec 7, 2017, 09:56 Jefrey Sobreira Santos notifications@github.com
wrote:

I'm following the docs to do the following:

var sessions = require('../models/sessions.js');
var users = require('../models/users.js');

    sessions.where('key', sess_key).fetchOne({withRelated: ['users']}).then(function(row) {
        callback(false, row.related('users'));
    }).catch(function(err) {
        callback(err);
    });

But this is what I get:

TypeError: sessions.where is not a function

sessions.js is:

module.exports = function() {
return global.db.bookshelf.Model.extend({
tableName: 'sessions',
hasTimestamps: true
});
}

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/bookshelf/bookshelf/issues/1704, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAJBTJ2ATdldflLofjBnLEkdQcm0vDkuks5s97ZCgaJpZM4Q5Rru
.

Closing this due to lack of activity. If you have anything more to add we can reopen this again.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chimmelb picture chimmelb  Â·  4Comments

KieronWiltshire picture KieronWiltshire  Â·  3Comments

ADrejta picture ADrejta  Â·  3Comments

Oxyrus picture Oxyrus  Â·  4Comments

casoetan picture casoetan  Â·  3Comments