mongodb - Strongloop/loopback remoteMethod save to databse -


so, i'm learning strongloop api, , extending api: https://docs.strongloop.com/display/public/lb/extend+your+api

i modified 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 data ']   } ); 

which works expected (at least shows in list):

enter image description here

things i'd have clarified , can't find anywhere in docs:

  1. how tell database connector (mongodb) put data in, because strongloop comes lot of default remotemethods, can't find anywhere how defined. , getting data works semi-automagically, setting seems obscure in documentations.
  2. i'm assuming should not talk database-connector, through loopback interface;
  3. how/where use methods node.updateattribute('data', 'test'), because inside node.setdata function undefined method. see part of persistedmodel but, use mystery me.
  4. why cb() undefined, while in example should work

i've tried verb post well, should not matter, want able write data db when request example.

i'm not first finds learning curve strongloop pretty steep. know of tutorials give me better understanding? i've been through core concepts , getting started, have feeling not got started @ all.

you can tell mongodb store things setting datasource points mongodb instance on local machine (or remote mongodb service mongolab):

assuming have mongod running locally , have installed mongodb connector, add datasources.json:

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

then need configure model definitions use mongods 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 more details.

the "loopback way" write data database instantiating , using models define in common/models folder. see models loopback ships default can inside node_modules/loopback/common/models folder. check out user.js file, it's how manage users of app , functions defined. that's "magic" is. ;)

in order use methods node.updateattributes(), need first instance of node model, using 1 of built in query methods node.findbyid() here:

node.findbyid(1, function(err, nodeinstance) {   if(err) return(err);   // nodeinstance node instance obj id = 1   // inside callback function    nodeinstance.updateattributes({name: 'fred'}, function() {...}) }); 

this assumes there exists entry node id of 1 name property, can create loopback rest explorer. in example code, setdata() remote method created kind of "class method", not have particular node instance associated it. in remote methods you'll need use model.find() , other methods instance result in order use instance methods on them.

https://docs.strongloop.com/display/public/lb/remote+methods

the cb callback argument function need call when remote method has either succeeded or failed, api can continue on processing , return successfully. success case,

cb(null, response); 

and error case,

cb(error, null); 

also, note code posted, have

node.setdata = function(response, cb) {...} 

instead of function(response, cb), should function(data, id), match remote method signature defined in

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

because remote method expecting 2 string arguments, 1 called data , other called id.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -