Search code examples
loopbackjsstrongloop

Unable to call one loopback app models in another looopback app using loopback-connector-remote


I have created two test loopback apps using lb cli

1) loopbacktest1
2) loopbacktest2

And I am using loopback-connector-remote to get access of loopbacktest2 models in loopbacktest1, but I could not access it and I could not even see app2 models in app1 at all(vice verse as well), Can anyone help me?

Here is my datasoureces

{
 "db": {
 "name": "db",
 "connector": "memory"
 },
"MyMicroService": {
 "name": "MyMicroService",
 "connector": "remote",
 "url": "http://localhost:7001/api"
 }
}

Updating question with final answer:-

Add below config in json file(this is the remote method name)

  todos.json
  "methods": {
    "getName": {
      "returns": {
      "arg": "data",
      "type": "string"
     },
     "http": {
       "verb": "get"
     }
   }
  }

And call that remote method like this

const remoteDs = ModelName.app.dataSources.MyMicroService;
  // the strong-remoting RemoteObjects instance
  const remotes = remoteDs.connector.remotes;
  remotes.auth = {
    bearer: `${access_token}`,
    sendImmediately: true
  };
  MyMicroService.app.models.todos.getName(function (err, data) {
    cb(err, data);
  });
  // cb(null, data);
}).catch((err) => {
  cb("sone error", null);
});

Here still I am facing some small issue, that is if above authentication fails then I am getting err as null and data as undefined, Instead I am expecting error value and data as null.There might be some issue in loopback-connector-remote


Solution

  • You should define loopbacktest2 models in loopbacktest1 as well, indicating that the datasource to be used is MyMicroService.

    If you follow the example at https://github.com/strongloop-community/loopback-example-connector/tree/remote then you should have a client subdirectory containing several files including model-config.json where you should add loopbacktest1 models with MyMicroService as datasource.

    And within common/models a json file for each of those models, containing at least the bare definition, something like https://github.com/strongloop-community/loopback-example-connector/blob/remote/common/models/person.json

    I've followed that example and got it working in no time.

    I've handled authentication this way:

    const app = require('./client/client');
    const User = app.models.User;
    const MyModel = app.models.MyModel;
    // the remote datasource (as defined in datasources.json)
    const remoteDs = app.dataSources.remoteDS;
    // the strong-remoting RemoteObjects instance
    const remotes = remoteDs.connector.remotes;
    /*
       credentials.json example (I keep it untracked by git):
    
       {
        "email": "[email protected]",
        "password": "123456"
       }
    
    */
    const credentials = require('./credentials.json');
    
    User.login(credentials).then(token => {
        // store the token to allow logout
        credentials.token = token;
    
        // set the access token to be used for all future invocations
        remotes.auth = {
            bearer: (new Buffer(token.id)).toString('base64'),
            sendImmediately: true
        };
    
        /* from this point every request made by any model attached 
           to remoteDS will be authenticated */
        return MyModel.find();
    }, err => {
        // handle auth error
    }).then(models => {
        console.log(`Got ${models.length} instances!`);
    });