0

So, I'm learning the strongloop API, and extending my API: https://docs.strongloop.com/display/public/LB/Extend+your+API

I modified the getName example:

Node.setData = function(response, cb) {
  console.log(response)      
  // cb(null, response);
};
Node.remoteMethod(
  'setData',
  {
    http: {path: '/:id/setData', verb: 'get'},
     accepts: [ 
       {arg:'data', type:'string'}, 
       {arg:'id', type:'string', required: true}
     ],
     returns: {arg: 'setData', type: 'string'},
     description: [ 'set some Data ']
  }
);

Which works as expected (at least it shows in the list):

enter image description here

Things I'd like to have clarified and can't find anywhere in the docs:

  1. How to tell my database connector (MongoDB) to put data in, because strongloop comes with a lot of default remoteMethods, but I can't find anywhere how they are defined. And getting data works semi-automagically, but setting seems really obscure in the documentations.
  2. I'm assuming I should not talk to the database-connector, but through a certain loopback interface;
  3. How/Where to use methods like Node.updateAttribute('data', 'test'), because inside the Node.setData function I get undefined method. I see it is part of persistedModel but, where I use this is a mystery to me.
  4. Why cb() is undefined, while in the example it should work

I've tried it with verb post as well, but this should not matter, i want to be able to write some data to the db when I get a request for example.

I'm probably not the first that finds the learning curve for Strongloop pretty steep. Anyone know of any tutorials that give me a better understanding? I've been through the core concepts and getting started, but I have the feeling that I not even got started at all.

Jee Mok
  • 6,157
  • 8
  • 47
  • 80
TrySpace
  • 2,233
  • 8
  • 35
  • 62

1 Answers1

1

You can tell MongoDB to store things by setting up a Datasource that points to a mongodb instance on your local machine (or a remote MongoDB service like MongoLab):

Assuming you have mongod running locally and have installed the mongodb connector, add this to datasources.json:

"mongoDS": {
  "host": "localhost",
  "port": 27017,
  "database": "databasename",
  "username": "dbo",
  "password": "password",
  "name": "mongoDS",
  "connector": "mongodb"
}

Then you need to configure all your model definitions to use the mongoDS as named above, inside model-config.json:

...snip...
"User": {
  "dataSource": "mongoDS",
  "public": true
},
"AccessToken": {
  "dataSource": "mongoDS",
  "public": false
},
"ACL": {
  "dataSource": "mongoDS",
  "public": false
},
...snip...

See https://docs.strongloop.com/display/public/LB/MongoDB+connector for more details.

The "loopback way" to write data to the database is by instantiating and using the models you define in your common/models folder. To see the models that loopback ships with by default you can look inside the node_modules/loopback/common/models folder. Check out the user.js file, it's how you will manage users of your app and where all the functions are defined. That's where the "magic" is. ;)

In order to use methods like node.updateAttributes(), you need to first get an instance of the Node model, using one of the built in query methods like Node.findById() here:

Node.findById(1, function(err, nodeInstance) {
  if(err) return(err);
  // nodeInstance is now the node instance obj with id = 1
  // inside this callback function

  nodeInstance.updateAttributes({name: 'Fred'}, function() {...})
});

This assumes there exists an entry for Node with an id of 1 with a name property, which you can create with the Loopback REST Explorer. In your example code, the setData() remote method you created is a kind of "Class Method", which does not have a particular Node instance associated with it. Even in remote methods you'll need to use Model.find() and other methods to get an instance result back in order to use the instance methods on them.

https://docs.strongloop.com/display/public/LB/Remote+methods

The cb callback argument is the function that you need to call when your remote method has either succeeded or failed, so that the API can continue on processing and return successfully. For the success case, do

cb(null, response);

and for the error case,

cb(error, null);

Also, just a note about the code you posted, where you have

Node.setData = function(response, cb) {...}

Instead of function(response, cb), it should be function(data, id), to match the remote method signature defined in

accepts: [ 
  {arg:'data', type:'string'}, 
  {arg:'id', type:'string', required: true}
]

because the remote method is expecting 2 string arguments, one called data and the other called id.

notbrain
  • 3,366
  • 2
  • 32
  • 42
  • Thanks, it seems because I used a `String` as an id, to test, it would return null, because it can't find it through that. Only through the built-in `ObjectId`. – TrySpace Jan 29 '16 at 09:15