Id from the DB, auto increment - BUG?


#1

Hello everyone,
I’m playing around with ionic tabs sample app. In services.js I changed data to come from my remote JSON. Here’s the code:

angular.module(‘starter.services’, [])

/**

.factory(‘Friends’, function($http) {
var friends = [];

$http.get(‘http://10.10.1.1/host4buck/index.php/example/friends’)
.success(function(data, status, headers,config){
console.log(‘data success’);
console.log(data); // object seems fine
})
.error(function(data, status, headers,config){
console.log(‘data error’);
})
.then(function(result){
friends = result.data;
});

return {
all: function() {
console.log(‘return’);
console.log(friends);
return friends;
},
get: function(friendId) {
// Simple index lookup
return friends[friendId];
}
}
})

The JSON file returns:

[
{“id”:“1”,“name”:“John Thomas",“email”:"45234@gmail.com”},
{“id”:“2”,“name”:“Kim Cha",“email”:"sdfh@gmail.com”},
{“id”:“3”,“name”:“Tim Aloha",“email”:"52345@gmail.com”},
{“id”:“4”,“name”:“Ka Mie",“email”:"sdf.sdf@gmail.com”},
{“id”:“5”,“name”:“Brad Lee",“email”:"25.2345@gmail.com”}
]

It all works except that, when I click a name from the list it jumps and shows the next entry’s details. So I guess it’s a bug or something? If I use another field and manually start id from 0 it all works just as expected.

What do you think?


#2

a array begins with zero, if you call the item 1

"{"id":"1","name":"John Thomas","email":"45234@gmail.com"}" 

it will show

"{"id":"2","name":"Kim Cha","email":"sdfh@gmail.com"}"

because this is the second item in the array…


#3

@maleeb Understand that. But don’t you think it’s kinda wrong to apply it here?


#4

u can use like “return friends[friendId - 1];”, than its working

//edit
but if you have a id jump in your database, than its not working.
its better if u dont call the array with the id from the database