use of then / catch in $http call in angular - javascript

I my Node backend have the following end-point:
usersRoute.get('/get', function(req, res) {
//If no date was passed in - just use todays date
var date = req.query.date || dateFormat(new Date(), 'yyyy-mm-dd'),
search = req.query.search;
users.getAllUsers(date, search)
.then(function(results) {
res.json(results);
}, function(err) {
res.status(500).json({
success: false,
message: 'Server error.',
data: []
});
});
});
I have changed my sql table name to something else to trigger the function(err){} part
When I use this in my service it looks like this:
function getUsers(date, search) {
return $http.get('/api/users/get', {
params: {
date: UtilsService.formatDate(date),
search: search
}
})
.then(getData)
.catch(handleErr);
function getData(response) {
return response.data;
}
function handleErr(err) {
LoggerService.error('Could not retrieve users.', err ,'Ooops');
}
}
Knowing the server will return an http status code 500, I thought it would go right to the catch block. But it also returns the data /which is undefined in the then block
I use my service in my controller like this:
function getUsers(date, search) {
isAdmin();
vm.loading = true;
vm.filteredUsers = [];
return UsersService.getUsers(date, search).then(function(data) {
vm.loading = false;
allUsers = data || [];
vm.filteredUsers = allUsers.slice(0, 50);
vm.distribution = UsersService.getDistribution(allUsers);
return vm.filteredUsers;
});
}
My problem is, since the then part is triggered in my service. I'm trying to slice undefined
My question is: What are som best practices when it comes to this sort of pattern.

The problem is that your catching the error from your API and then returning the promise created by .catch.
Quick example
promise.then(function(data) {
throw 'Some error';
}).catch(function (err) {
console.log(err) // will output 'Some error'
}).then(function () {
// This will run even though we have a catch before
});
So how can we prevent the .then it's easy we throw an error inside the .catch
promise.then(function(data) {
throw 'Some error';
}).catch(function (err) {
console.log(err) // will output 'Some error'
throw 'You shall not pass'
}).then(function () {
// This will not run
});
So in your case you have two options, one throw an error as I said or two inject the $q service into your service:
function getUsers(date, search) {
return $http.get('/api/users/get', {
params: {
date: UtilsService.formatDate(date),
search: search
}
})
.then(getData)
.catch(handleErr);
function getData(response) {
return response.data;
}
function handleErr(err) {
LoggerService.error('Could not retrieve users.', err ,'Ooops');
return $q.reject(err);
}
}

You could do something like that
function getUsers(date, search, cb) {
return $http.get('/api/users/get', {
params: {
date: UtilsService.formatDate(date),
search: search
}
})
.then(cb)
.catch(handleErr);
function handleErr(err) {
LoggerService.error('Could not retrieve users.', err ,'Ooops');
}
}
And then in your controller
UsersService.getUsers(date, search, function(data) {
vm.loading = false;
allUsers = data || [];
vm.filteredUsers = allUsers.slice(0, 50);
vm.distribution = UsersService.getDistribution(allUsers);
});

Related

value not setting in the variable which is defined outside in NodeJS

I'm new to NodeJS so please apologize if below code is not up-to the standard. I would like to access isSuccess value outside of this function stepfunctions.listExecutions
I tried below code but I'm getting the value is undefined not getting the expected output. I did some internet search and came to know in NodeJS we can't set the value which is defined in globally but I've use case and I'm pretty sure this is a common case for others too - where I would like to access this isSuccess value after my execution.
const AWS = require('aws-sdk');
const stepfunctions = new AWS.StepFunctions({
region: process.env.AWS_REGION
});
var params = {
stateMachineArn: 'arn:aws:states:us-west-1:121:stateMachine:test',
maxResults: '2',
nextToken: null,
statusFilter: 'SUCCEEDED'
};
var isSuccess
stepfunctions.listExecutions(params, function (err, data) {
if (err) console.log(err, err.stack);
else
data.executions.forEach(function (result) {
let params = {
executionArn: result.executionArn
};
stepfunctions.describeExecution(params, function (err, data) {
if (err) console.log(err, err.stack);
else {
isSuccess = 'true'
}
});
});
console.log('isSuccess: ' +isSuccess)
});
Expected output:
isSuccess: true
But I'm getting
isSuccess: undefined
Could you please help me to resolve this issue. Appreciated your help and support on this.
This is how you can wrap it on promise
let isSuccess;
const listExecute = function(params) {
return new Promise((resolve, reject) => {
stepfunctions.listExecutions(params, function (err, data) {
if (err) reject(err);
else
data.executions.forEach(function (result) {
let params = {
executionArn: result.executionArn
};
stepfunctions.describeExecution(params, function (err, data) {
if (err) reject(err);
else {
resolve(true)
}
});
});
});
})
}
async function getOutout(params) {
try {
isSuccess = await listExecute(params);
console.log(isSuccess, 'Output')
} catch(e) {
console.log(e)
}
}
getOutout(params)
Also you can export the listExecute so that you can use this function outside of this file.
module.exports = {listExecute}

How to test callback of function in Chai?

I am using Slack API and I want to test does it work fine with response status code. Here is sending function :
sendMsg(msg) {
return this.slack.webhook({text: msg}, (err, res) => {
if (err) {
throw err;
}
console.log(res.statusCode) // = 200
return res.statusCode;
});
}
And my test:
it('Checks connection with Slack', (() => {
let slack = new Slack();
let res = slack.sendMsg('test');
expect(res).to.equal(200);
}));
But ofc. it's giving me request object to slack. I want to wait for response object from slack API. Thanks in advance.
It looks like slack.webhook takes in a callback, which is how you retrieve the status. The problem is that the caller of sendMsg has no way of getting that status.
One way to solve this is to have sendMsg take in a callback:
sendMsg(msg, onStatusReceived) {
this.slack.webhook({text: msg}, (err, res) => {
if (err) {
throw err;
}
console.log(res.statusCode) // = 200
onStatusReceived(res.statusCode);
});
}
Then in your test, use done to end the test when the callback is invoked:
it('Checks connection with Slack', (done) => {
let slack = new Slack();
slack.sendMsg('message', status => {
expect(status).to.equal(200);
done();
});
});
Another way is to have sendMsg wrap slack.webhook in a promise, so the caller can do sendMsg().then(...).
one of the ways I handled a returning callback to test is as follows:
it('receives successful response', async () => {
nock('https://localhost')
.persist()
.log(console.log)
.post(‘/getData’, (unitData, callback) => {
return true;
})
.delayBody(1000)
.reply(200, {statusCode: 'Some Status'});
const getSomeData = await getResponse(unitData, function callBack(unitData, error, data){
expect(data.statusCode).to.be.equal(200);
}) })
getResponse Function (returning callback):
getResponse(unitData, function callBack(unitData, error, data){
try {
return request.post(unitData, function (err, resp) {
if (!err && resp.statusCode === 200) {
if (resp.body.error) {
return callback(obj, JSON.stringify(resp.body.error), null);
}
return callback(obj, null, resp);
} else {
if (err == null) {
err = { statusCode: resp.statusCode, error: 'Error occured.' };
}
return callback(obj, err, null);
}
});
} catch (err) {
return callback(obj, err, null);
}
}

Possible Promise Issue

Hi this works for the first execution but then when I try again it seems to break, does anything seem wrong with the way the promises are used here
socket.on('new user', function(data, callback) {
return getUsersFromDb()
.then(function() {
if (users.name.indexOf(data) > -1) {
callback(false);
reject(err);
}
else {
callback(true);
// socket.nickname = data;
// people[socket.nickname] = socket;
updateNicknames();
var user = new User();
user.name = data;
user.save(function(err, data) {
if (err) {
console.log(err)
}
else {
return getUsersFromDb()
.then(function() {
updateNicknames()
})
.catch(function(e) {
console.log("Error", e)
})
}
});
socket.broadcast.emit('user join', {nick: socket.nickname});
}
})
.catch(function(e) {
console.log("Error", e)
})
});
Sublime seems to colour the then and catch inside the second else differently as if it was used wrongly
I'm assuming the following function returns a promise!?
getUsersFromDb()
Have you tried jQuery promise, using $.when(functionWhichReturnsAPromise()).then( "the rest of the code")?

Use ldapjs with promise

I want to convert the following code to use promise. It is working and output a user's attributes within the active directory.
var client = ldap.createClient({
url: ldap_url
});
client.bind(ldap_username, ldap_password, function (err) {
client.search(ldap_dn_search, opts, function (err, search) {
search.on('searchEntry', function (entry) {
var user = entry.object;
// It is working!!!. It outputs all user attributes.
console.log(user);
});
});
});
The following is my attempt, butit doesn't output anything.
var Promise = require('promise');
var client_bind = Promise.denodeify(client.bind);
var client_search = Promise.denodeify(client.search);
client_bind(ldap_username, ldap_password)
.then(function(err){
client_search(ldap_dn_search, opts)
.then(function(search){
var search_on = Promise.denodeify(search.on);
search_on('searchEntry')
.then(function(entry){
var user = entry.object;
// It doesn't output anything !!!
console.log(user);
});
});
});
I had the same problem.
Search emits events, so we need something that processes them and passes further along the chain.
Here is piece of code, that works for me:
var ldap = require('ldapjs');
var promise = require('bluebird');
var client = ldap.createClient({url: app.settings['ldap']['server']});
var uid;
promise.promisifyAll(client);
function searchPromise(res, notfoundtext) {
return new Promise(function(resolve, reject) {
var found = false;
res.on('searchEntry', function(entry) {
found = true;
resolve(entry);
});
res.on('error', function(e) {
reject(e.message);
});
res.on('end', function() {
if (!found) {
reject(notfoundtext);
}
});
});
}
client.searchAsync(app.settings['ldap']['baseDn'], {filter: '(mail='+credentials.email+')', scope: 'sub'})
.then(function(res) {
return searchPromise(res, 'User isn\'t exists.');
})
.then(function (entry) {
uid = entry.object.uid;
return client.bindAsync(entry.object.dn, credentials.password);
})
.then(function() {
return client.searchAsync('cn='+app.settings['ldap']['group']+',cn=groups,'+app.settings['ldap']['baseDn'], {scope: 'sub', filter: '(memberUid='+uid+')'});
})
.then(function(res) {
return searchPromise(res, 'User is not in group ' + app.settings['ldap']['group']);
})
.then(function() {
console.log('All is ok');
})
.catch(function(message) {
console.log('Error:' + message);
});
Immediately after the search I add one more step that catches the events, processes them, and passes it further along the chain. This makes the function searchPromise.
Good luck coding )
Most likely those methods do require to be called on client as a context, so you will need to bind() them before passing them to Promise.denodeify:
var client_bind = Promise.denodeify(client.bind.bind(client));
var client_search = Promise.denodeify(client.search.bind(client));
Also, a proper use of promises would look like this:
client_bind(ldap_username, ldap_password).then(function() {
return client_search(ldap_dn_search, opts);
// ^^^^^^ always return something from the callback
}).then(function(search) { // flatten your chain
return Promise.denodeify(search.on).call(search, 'searchEntry');
// ^^^^^^ an alternative to `bind`
}).then(function(entry){
var user = entry.object;
console.log(user);
}).catch(function(err) { // always catch errors!
console.error(err);
});
Using Bluebird Promises, the easy way to do this is to create your client normally, and then run the promisifyAll() on the client.
var ldap = require('ldapjs');
var Promise = require('bluebird');
var client = ldap.createClient({
url: 'ldap://my-server:1234',
});
Promise.promisifyAll(client);
Now you can call client.addAsync() and client.searchAsync() and such.
client.bindAsync(secUserDn, secUserPassword)
.then(doSearch) // if it works, call doSearch
.catch(function (err) { // if bind fails, handle it
console.error('Error on bind', err)
});
function doSearch(data) {
client.searchAsync('CN=A Test,OU=Users,DC=website,DC=com', options)
.then(function (data) { // Handle the search result processing
console.log('I got a result');
})
.catch(function (err) { // Catch potential errors and handle them
console.error('Error on search', err);
});
}
i had the same issue here but i solved it by adding promise and resolve the response without using bluebird, this is an exemple of my code :
async getLdapUser(username: any): Promise<any> {
let myPromise = new Promise<boolean>((resolve, reject) => {
console.log('ssssssssss', username);
const adSuffix = 'OU=xxxx,OU=xxxxx,DC=xxxxxxx,DC=xxxxxx';
const password = 'xxxxxxxxxxxxx';
// Create client and bind to AD
const client = ldap.createClient({
url: 'ldap://1.1.1.1:389',
});
// promise.promisifyAll(client);
let resp = false;
// console.log(client);
client.bind('userTest', password,(err: any) => {
console.log('RESP', resp);
if (err) {
console.log('Error in new connetion ' + err);
} else {
/*if connection is success then go for any operation*/
console.log('Success');
const searchOptions: {} = {
scope: 'sub',
filter: '(sAMAccountName=' + username + ')',
attributes: ['sAMAccountName'],
};
client.search(adSuffix, searchOptions, (err: any, res: any) => {
assert.ifError(err);
res.on('searchEntry', (entry: any) => {
resp = true;
});
res.on('error', (error: any) => {
console.log('err');
reject(error.message);
});
await res.on('end', (result: any) => {
resolve(resp);
});
});
}
});
});
return myPromise;
}

persist data in a DB issue, receiving NULL on console

I am new to NodeJS and I am having an issue trying to persist/save some data in a DB.
let's start from the beginning so you can understand easier. I have a list of sports with an option to checked or unchecked, that's what I need to persist, that checked.
FRONT END:
controller.js
$scope.toggleSportSelection = function(sport) {
var params = {};
params.user = $scope.customer.customer;
sport.checked = !sport.checked;
SportsFactory.setSportChecked(params);
};
service.js
setSportChecked: function(params) {
var defer = $q.defer();
$http.post(CONSTANT_VARS.BACKEND_URL + '/sports/checked', params)
.success(function(sportChecked) {
LocalForageFactory.remove(CONSTANT_VARS.LOCALFORAGE_SPORTS_CHECKED, params);
defer.resolve(sportChecked);
})
.error(function(err) {
console.log(err);
defer.reject(err);
});
return defer.promise;
}
I've been debugging this front end part and everything seems to be OK...
Now BACK END:
setSportCtrl.js
module.exports = {
setCheck: function(req, res) {
var checkedSportParams = req.body;
SportSelectionService.sportChecked(checkedSportParams).then(function() {
res.json(200, {msg: 'OK'});
}, function(err) {
res.json(400, err);
});
}
}
SportSelection.js (model)
module.exports = {
connection: 'RedisServer',
attributes: {
sport: {
type: 'array',
required: false
},
user: {
type: 'string',
required: true
}
}
};
in this part I can see how that console are print in the terminal, but if I do console.log(sportChecked) or console.log(newSport) all I get is an array which says null everywhere...
SportSelectionService.js
module.exports = {
sportChecked: function(params) {
var Promise = require('bluebird');
return new Promise(function(fullfill, reject) {
console.time('sportChecked_findOne');
SportSelection.findOne({
user: params.user
}).exec(function(err, sportChecked) {
console.timeEnd('sportChecked_findOne');
var newSport;
if (err) {
reject(new Error('Error finding user'));
console.error(err);
}else if (sportChecked) {
newSport = sportChecked.sport;
console.time('sportChecked_update');
SportSelection.update({
user: params.user
},
{
sport: newSport
}).exec(function(err, sportCheckedUpdated) {
console.timeEnd('sportChecked_update');
if (err) {
reject(new Error('Error on sportChecked'));
}else {
fullfill(sportCheckedUpdated);
}
});
if (sportChecked.sport) {
sportChecked.sport.push(params.sport);
console.log('New sport added');
}else {
sportChecked.sport = [params.sport];
}
}else {
console.time('sportChecked_create');
SportSelection.create({
sport: [params.sport],
user: params.user
}).exec(function(err, created) {
console.timeEnd('sportChecked_create');
if (err) {
reject(new Error('Error on sportChecked'));
}else {
fullfill(created);
}
});
}
});
});
}
So what do you think is my issue here ? what am I doing wrong ?
here is the way I did it, I will teach how from the beginning to the end
starting from the Node.js part, I am using Sails.js and lodash
SetSportsController.js
'use strict';
module.exports = {
setCheck: function(req, res) {
var checkedSportParams = req.body;
SportSelectionService.sportChecked(checkedSportParams).then(function() {
res.json(200, {msg: 'OK'});
}, function(err) {
res.json(400, err);
});
},
retrieveSetCheck: function(req, res) {
if (req.params) {
SportSelectionService.getSportChecked(req.params).then(function(sportChecked) {
res.json(200, sportChecked);
}, function(err) {
res.json(400, err);
});
}else {
res.json(400, {error: 'Error retrieving Sports'});
}
}
};
than we go with SportSelectionService.js
'use strict';
var _ = require('lodash');
module.exports = {
sportChecked: function(params) {
var Promise = require('bluebird');
return new Promise(function(fullfill, reject) {
SportSelection.findOne({
user: params.user
}).exec(function(err, sportChecked) {//this array comes with duplicates
var newSport,
sportCheckedUniq = _.uniq(sportChecked.sport);//prevents duplicates
if (err) {
reject(new Error('Error finding user'));
console.error(err);
}else if (sportChecked) {
newSport = sportCheckedUniq || [];
if (_.includes(sportCheckedUniq, params.sport)) {
sportCheckedUniq = _.pull(newSport, params.sport);
sportCheckedUniq = _.difference(newSport, params.sport);
}else {
newSport.push(params.sport);
sportCheckedUniq = newSport;
}
SportSelection.update({
user: params.user
},
{
sport: newSport
}).exec(function(err, sportCheckedUpdated) {
if (err) {
reject(new Error('Error on sportChecked'));
}else {
fullfill(sportCheckedUpdated);
}
});
if (sportCheckedUniq) {
sportCheckedUniq.push(params.sport);
}else {
sportCheckedUniq = [params.sport];
}
}else {
SportSelection.create({
sport: [params.sport],
user: params.user
}).exec(function(err, created) {
if (err) {
reject(new Error('Error on sportChecked'));
}else {
fullfill(created);
}
});
}
});
});
},
getSportChecked: function(params) {
var Promise = require('bluebird');
return new Promise(function(fullfill, reject) {
console.time('sportChecked_findOne');
SportSelection.findOne({
user: params.user
}).exec(function(err, sportChecked) {
console.timeEnd('sportChecked_findOne');
if (err) {
reject(new Error('Error finding sportChecked'));
console.error(err);
}else {
if (sportChecked) {
fullfill(sportChecked);
}else {
SportSelection.create({
// 10 is the ID for soccer, which must unchecked by default on every single user.
sport: [10],
user: params.user
}).exec(function(err, created) {
console.log(err);
console.log(created);
if (err) {
reject(new Error('Error on sportChecked'));
}else {
fullfill(created);
}
});
}
}
});
});
}
};
as you can see here we have only 2 methods, the first
sportChecked() is the one which fires up when the user checked or unchecked any of the items.
and then we have getSportChecked() which is the method called everytime that the user logs in again.
I do not have any delete method because we are not deleting anything, we are just watching for a change of statement.
Also I am working with a Redis Server
do not forget to create the model, I gave'em a name SportSelection.js
'use strict';
module.exports = {
connection: 'RedisServer',
attributes: {
sport: {
type: 'array',
required: false
},
user: {
type: 'string',
required: true
}
}
};
also, in the config folder we have policies.js, I can't tell you how to work with this because is your configuration, but mine is:
SetSportsController: {
setCheck: ['jwtAuth', 'sanitizerPolicy', 'headersPolicy'],
retrieveSetCheck: ['jwtAuth', 'sanitizerPolicy']
},...
then, we go to the Front End Part (remember: AngularJS)
I have a controller, controller.js
$scope.toggleSportSelection = function(sport) {
SportsFactory.setSportChecked({
user: $scope.customer.customer,
sport: sport.id
}).then(function() {
sport.checked = !sport.checked;
$ionicScrollDelegate.resize();
}, function() {
$ionicScrollDelegate.resize();
});
};
which is working along this template
<ion-item ng-repeat="sport in sportsFilter track by $index"
ng-click="toggleSportSelection(sport)">
{{:: sport.name}}
</ion-item>
then, service.js
be aware of AngularJS
here is where I make the post and get, look
.factory('SportsFactory', function($http, $q, AuthFactory, LocalForageFactory,
LeaguesFactory, ImageFactory, CONSTANT_VARS) {
getSports: function(customer) {
var defer = $q.defer(),
_this = this;
LocalForageFactory.retrieve(CONSTANT_VARS.LOCALFORAGE_SPORTS)
.then(function(sports) {
if (!_.isNull(sports)) {
defer.resolve(sports);
}else {
$http.get(CONSTANT_VARS.BACKEND_URL + '/lines/sports/' + customer.agent)
.success(function(sports) {
sports = _.sortBy(sports, function(sport) {
return sport.priority;
});
_this.getSportChecked(customer).then(function(sportChecked) {
var sportIds = _.pluck(sports, 'id'),
intersectedSports = _.intersection(sportIds, sportChecked.sport);
if (sports.length) {
sports = _.map(sports, function(sport) {
sport.checked = !_.includes(intersectedSports, sport.id);
return sport;
});
}else {
AuthFactory.logout();
}
});
_.each(sports, function(sport) {
var sportImg = ImageFactory.sportImages(sport);
if (sportImg.length) {
sport.img = sportImg[0];
}else {
sport.img = 'https://placehold.it/40x40';
}
});
defer.resolve(sports);
})
.error(function(err) {
defer.reject(err);
});
}
});
return defer.promise;
},
setSportChecked: function(params) {
var defer = $q.defer();
$http.post(CONSTANT_VARS.BACKEND_URL + '/sports/checked', params)
.success(function(sportChecked) {
LocalForageFactory.remove(CONSTANT_VARS.LOCALFORAGE_SPORTS_CHECKED, params);
defer.resolve(sportChecked);
})
.error(function(err) {
console.log(err);
defer.reject(err);
});
return defer.promise;
},
getSportChecked: function(customer) {
var defer = $q.defer(),
user,
rejection = function(err) {
defer.reject(err);
};
LocalForageFactory.retrieve(CONSTANT_VARS.LOCALFORAGE_SPORTS_CHECKED)
.then(function(sportChecked) {
user = customer.customer;
if (!_.isNull(sportChecked)) {
defer.resolve(sportChecked);
}else {
$http.get(CONSTANT_VARS.BACKEND_URL + '/sports/getChecked/' + user)
.success(function(sportChecked) {
LocalForageFactory.set(CONSTANT_VARS.LOCALFORAGE_SPORTS_CHECKED, sportChecked);
defer.resolve(sportChecked);
})
.error(rejection);
}
}, rejection);
return defer.promise;
}
});
first, center your attention to setSportChecked() and getSportChecked(), there is where the magic happen in this service, then the function getSports() make a called to getSportChecked() which looks like this
_this.getSportChecked(customer).then(function(sportChecked) {
var sportIds = _.pluck(sports, 'id'),
intersectedSports = _.intersection(sportIds, sportChecked.sport);
if (sports.length) {
sports = _.map(sports, function(sport) {
sport.checked = !_.includes(intersectedSports, sport.id);
return sport;
});
}else {
AuthFactory.logout();
}
});
so, this is the end version if this long project, you have to touch lots of file to get with this, to save/persist data in a DB, so, see this code because this is how I have it so far and is working great and is fast, I have no errors yet, parting from here, ask the questions you need to know, I will be answering during the day. Hope this helps
IF you are using mongooseJS (it appears you are) AND the purpose of the the service is to add the sport to the sports array then you can use the findOneAndUpdate method (which will return a promise with the exec method) and significantly reduce the service to:
module.exports = {
sportChecked: function(params) {
return SportSelection.findOneAndUpdate(
{user: params.user},
{$addToSet: {sports: params.sport}}
).exec();
}
};
$addToSet will only add the value if it is not already in the array. If duplicates are acceptable you can use $push
As pointed out in the comments you are likely using waterline. If so, it appears the update method behaves similarly to findOneAndUpdate. So maybe this might work (I didn't check if you need to call exec or not):
module.exports = {
sportChecked: function(params) {
return SportSelection.update(
{user: params.user},
{$addToSet: {sports: params.sport}}
).exec(); // Not sure if this is necessary
}
};
I think this could be done far more simply - rather than updating it like that, just track all your checkboxes client-side and update the whole thing when it changes.
I think you are missing out on some stuff that angular can do for you really easily, and in this case you can push all the workload off on the client rather than the server.
Firstly, make your HTML something like this:
<form name="myForm">
Basketball <input type="checkbox" ng-change="updateRecord()" ng-model="sport.basketball"><br />
Baseball <input type="checkbox" ng-change="updateRecord()" ng-model="sport.baseball"><br />
Football <input type="checkbox" ng-change="updateRecord()" ng-model="sport.football"><br />
Soccer <input type="checkbox" ng-change="updateRecord()" ng-model="sport.soccer"><br />
Golf <input type="checkbox" ng-change="updateRecord()" ng-model="sport.golf"><br />
<br />{{sport}}
<br /><span ng-show="loading">Updating Redis</span>
</form>
Plunker
Angular will create a sport object for you that will track whatever is or isn't checked. Rather than trying to manage that inside the database, just let angular take care of it, and whenever it changes, overwrite the whole record.
You can use your toggle function in the ng-change of your checkboxes:
$scope.toggleSportSelection = function(sport) {
var params = {};
params.user = $scope.customer.customer;
params.sport = sport
SportsFactory.setSportChecked(params);
};
On the backend, you can do similarly to what was suggested in other answers:
module.exports = {
sportChecked: function(params) {
return SportSelection.update(
{user: params.user},
{sports: params.sport}}
).exec();
}
};
This way you have way less code, it's easier to read, and you remove a LOT of logic from the server.
Unless there is some reason I am missing, you don't need to use $q in your ajax call. You can just use $http built in success and error to handle the promises.
You will need to change your model, by the way, to hold the sport object rather than an array.
Also, if you are in fact using sails, just use the blueprint PUT. You don't even need any logic - sails has already generated it when you generate your api.
When you load your page, just get the record back, and push data.sport into $scope.sport and your checkboxes should update.

Categories