node.js - How to use Promises/Chaining with Azure Mobile Services Custom API in Javascript -
node.js - How to use Promises/Chaining with Azure Mobile Services Custom API in Javascript -
i trying figure out how utilize promises ams javascript api.
these 2 functions have created 'promised'
class="snippet-code-js lang-js prettyprint-override">function checkusername(username, table) { homecoming table.where({username: username}).read({ success: function (results) { if (results.length === 0) { homecoming true; } else { homecoming false; } }, error: function(error) { homecoming false; } }); } function checkemail(email, table) { homecoming table.where({email: email}).read({ success: function (results) { if (results.length === 0) { homecoming true; } else { homecoming false; } }, error: function(error) { homecoming false; } }); }
class="snippet-code-js lang-js prettyprint-override">checkusername(body.username, accountstable).then(function (results) { if (results) { homecoming checkemail(body.email, accountstable); } else { response.send(400, {message: 'this username in use.'}); } }).then(function(results) { if (results) { response.send(200, {message: 'can proceed sign up.'}); } else { response.send(400, {message: 'this email address in use.'}); } });
i trying utilize promises in parse, it's not working. console logs maintain spitting out internal server error , .then() not function of object. i'm assuming missing require or in order have promises functionality?
error in script '/api/register.js'. typeerror: cannot phone call method 'done' of undefined @ exports.post (d:\home\site\wwwroot\app_data\config\scripts\api\register.js:30:59) [external code]
i have realized doing wrong.
i have decided utilize q node module promises.
class="snippet-code-js lang-js prettyprint-override">var q = require('q'); exports.post = function(request, response) { // utilize "request.service" access features of mobile service, e.g.: // var force = request.service.push; var tables = request.service.tables; var accountstable = tables.gettable('accounts'); var params = request.body; checkusername(params.username, accountstable).then(function (result) { if (result.length === 0) { homecoming checkemail(params.email, accountstable); } else { response.send(400, {message: 'this username in use.'}); } }).then(function (results) { if (results.length === 0) { return; } else { response.send(400, {message: 'this email address registered.'}); } }).then(function () { response.send(200, {message: 'username , email unique. can register!'}); }); }; function checkusername(username, table) { var deferred = q.defer(); table.where({username: username}).read({ success: function (result) { deferred.resolve(result); }, error: function (error) { deferred.reject(error); } }); homecoming deferred.promise; } function checkemail(email, table) { var deferred = q.defer(); table.where({email: email}).read({ success: function (result) { deferred.resolve(result); }, error: function (error) { deferred.reject(error); } }); homecoming deferred.promise; }
javascript node.js azure
Comments
Post a Comment