0
votes

I am trying to create the Profile model in loopback it is showing the error.

error details Unhandled error for request POST /api/Users: Error: Cannot call Profile.create(). The create method has not been setup. The PersistedModel has not been correctly attached to a DataSource!

 'use strict';

    var loopback = require('loopback');
    var boot = require('loopback-boot');

    var app = module.exports = loopback();

    app.start = function() {
      // start the web server
      return app.listen(function() {
        app.emit('started');
        var baseUrl = app.get('url').replace(/\/$/, '');
        console.log('Web server listening at: %s', baseUrl);
        if (app.get('loopback-component-explorer')) {
          var explorerPath = app.get('loopback-component-explorer').mountPath;
          console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
        }
      });
    };

    // Bootstrap the application, configure models, datasources and middleware.
    // Sub-apps like REST API are mounted via boot scripts.
    boot(app, __dirname, function(err) {
      if (err) throw err;

      // start the server if `$ node server.js`
      if (require.main === module)
        app.start();
    });
    console.log(Object.keys(app.models));

    app.models.User.afterRemote('create',(ctx,user,next)=>{

     console.log("The new User is ",user);


    app.models.Profile.create({
      first_name :user.username,
       created_at :new Date(),
       userId: user.id
    }, (err,result)=>{
      if(!err && result)
      {
        console.log("Created new profile !", result);
      }
      else{
        console.log("There is an error ",err);
      }
      next();
    });

    });

Prfile.JSON file

{
  "name": "Profile",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "first_name": {
      "type": "string"
    },
    "last_name": {
      "type": "string"
    },
    "birth_date":{
      "type":"date"
    },
    "created_at": {
      "type": "date"
    },
    "age": {
      "type": "number"
    },
    "history":{
      "type":["object"]
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

**This file is the profile.js file **

'use strict';

module.exports = function(Profile) {

};
2

2 Answers

1
votes

Perhaps you still need to bind your model to the data source in the model-config.json file, like that:

datasources.json

  "rdb": {
    "host": "localhost",
    "port": 3306,
    "database": "asat",
    "password": "12345",
    "name": "rdb",
    "user": "admin",
    "connector": "mysql"
  }
  ...

model-config.json

  "Profile": {
    "dataSource": "rdb",
    "public": true
  }
  ...
0
votes

The reason can be any of the follwoing

  • Forget to add the model name in the model-config.json file
  • Sometime when the loopback server start the model didnt get appended to the server object properly. If the model is not appended properly we can not use the model using the app object and one server restart will solve the issue.
  • Datasource is not correctly mentioned in model-config file