How to create loopback component with PersistedModel?

36 views
Skip to first unread message

Rastio Hodul

unread,
Dec 1, 2016, 2:38:54 PM12/1/16
to LoopbackJS
Hi,
I'm trying to create a loopback component that would have a PersistedModel.

Trying to do the same as loopback-component-oauth2, but models are not migrated to DB.

They do show up in loopback explorer but any call to GET results in:
"Cannot call Postcard.find(). The find method has not been setup. The PersistedModel has not been correctly attached to a DataSource!",

Here is my code in component's index.js:
'use strict';

var loopback = require('loopback');
var DataModel = loopback.PersistedModel || loopback.DataModel;

function loadModel(jsonFile) {
    var modelDefinition = require(jsonFile);
    return DataModel.extend(modelDefinition.name,
        modelDefinition.properties,
        {
            relations: modelDefinition.relations,
        });
}

module.exports = function (app, options) {
    var PostcardModel = loadModel('./models/postcard.json');
    var Postcard = require('./models/postcard.js')(PostcardModel);
    Postcard.dataSource = app.datasources.db;
    Postcard.public = true ;
    app.model(Postcard);
};


Are there any guidelines to do this?

Thanks.

Rastio Hodul

unread,
Dec 1, 2016, 4:29:57 PM12/1/16
to LoopbackJS
Figured it out. This is important part:
    app.model(Postcard, {
        'dataSource': 'db',
        'public': true,
    });

Reply all
Reply to author
Forward
0 new messages