You can use the following link to see a diff of how I added callbacks AND promise support to MyModel.js
file:
Last active
December 22, 2015 21:41
-
-
Save pulkitsinghal/dc8fa7c5c7b71fef5542 to your computer and use it in GitHub Desktop.
LoopBack: How to change a remote method to handle both callbacks and promise based invocations?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var loopbackUtils = require('loopback/lib/utils'); // handle both callbacks and promise based invocations | |
module.exports = function(MyModel) { | |
MyModel.remoteMethod('generateToken', { | |
accepts: [ | |
{arg: 'id', type: 'string', required: true} | |
], | |
http: {path: '/:id/generateToken', verb: 'get'} | |
}); | |
MyModel.generateToken = function(id, cb) { | |
log('inside generateToken'); | |
cb = cb || loopbackUtils.createPromiseCallback(); // handle both callbacks and promise based invocations | |
MyModel.app.models.AccessToken.createAccessTokenId(function(err, token){ | |
if (err){ | |
log('inside generateToken', 'ERROR', err); | |
cb(err); | |
} | |
else { | |
log('inside generateToken', 'token:', token); | |
MyModel.updateAll( | |
{id: id}, | |
{ token : token }, | |
function(err) { | |
if (err){ | |
log('inside generateToken', 'ERROR', err); | |
cb(err); | |
} | |
else { | |
log('inside generateToken', 'saved token in MyModel'); | |
cb(null); | |
} | |
} | |
); | |
} | |
}); | |
return cb.promise; // handle both callbacks and promise based invocations | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment