0

I'm trying to retrieve a list of users from a json generated from a url. I'm trying to adapt the code sugested on Angular UI select : Fetch data from remote service this way:

$scope.person = {};
$scope.people = {};
$http.get('https://sampleurl/pessoas/v1/?nome=' + item.name).then(
    function (response) {
      $scope.people = response.data;
      console.log(response)
    },
    function () {
      console.log('ERROR!!!');
    }
);

However, the input just breaks, I cant even type anything ont it :(

I had to change the real url since the json contains personal data.

UPDATE i removed item.name from the url and forced a value for name. Now i can get some fixed results, but i'm failling to add them to the input:

$scope.person = {};
  $scope.people = [];
  //e lá vamos nós
  $http.get('https://someurl/pessoas/v1/?nome=monteiro').then(
    function (response) {
      $scope.people = response.data;
      console.log(response)
    },
    function () {
      console.log('ERROR!!!');
    }
  );

getting the following error when i pick someone: Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: $item in $select.selected, Duplicate key: undefined:undefined, Duplicate value: undefined

UPDATE 2:

fixed the last error by defining all attributes from the object:

$scope.tagTransform = function (newTag) {
    var item = {
        nome: newTag,
        email: 'unknown',
        cpf: 'unknown',
        matricula: 'unknown',
        lotacao: 'unknown'
    };

    return item;
  };

Now i can pick someone, but the pick box has nothing inside. Also, i still cant search for string. I tried https://sampleurl/pessoas/v1/?nome=' + item.name and https://sampleurl/pessoas/v1/?nome=' + $selected.search but none worked.

3
  • is this response.data an array? Commented Mar 21, 2016 at 17:49
  • example: [{"nome":"dude","matricula":"32107","cpf":"12345678901","email":"[email protected]","lotacao":"SUPDE/DERJO/DE701","regional":"SERPRO - REGIONAL RIO DE JANEIRO"},{"nome":"girl","matricula":"46213","cpf":98765432101","email":"[email protected]","lotacao":"OPRJO/OPSIN/OPESE","regional":"SERPRO - REGIONAL RIO DE JANEIRO"}]; Commented Mar 21, 2016 at 17:52
  • notice "nome", is it a typo of "name"? I have edited my answer to use 'nome' Commented Mar 21, 2016 at 18:19

3 Answers 3

0

change $scope.people = response.data; to $scope.people = response;

Sign up to request clarification or add additional context in comments.

Comments

0

Assume you have the html like this, and the result.data is an array of person. I have {{people}} for debugging purpose. it should show the result.data you post in the comment

{{people}}

<ui-select multiple ng-model="person" >
   <ui-select-match placeholder="Select person...">{{$select.selected.nome}}</ui-select-match>
   <ui-select-choices repeat="person in people ">
     {{person.nome}}
   </ui-select-choices>
</ui-select>

$http.get('https://sampleurl/pessoas/v1/?nome=' + item.name).then(
  function (response) {
    $scope.people = response.data;
    console.log(response)
  },

  function () {
   console.log('ERROR!!!');
  }
 );

3 Comments

It still doesnt work. Checking the log, i noticed i'm getting "Error: item is not defined"..
same thing, Error: item is not defined. should i just upload the whole code somewhere? i'm feeling hopeless.
i am wondering if it is the item in your $http.get not defined. $http.get('sampleurl/pessoas/v1/?nome=' + item.name)
0

It's hard to recreate the issue to test exactly what is going on, but based on your comment to the original question, the returned data is in an array. So in order to get the JSON, you have to do the following:

$scope.people = response.data[0]

I always find it helpful to console log the response data and go through the hierarchy of the object and its children in the console. That way you can find out the order of the properties (if needed) which makes it easier to access the data you need: console.log(response.data)

6 Comments

this way i was able to type something, but it still doesnt work.
What is the console logging?
Error: item is not defined
What if you log response and response.data. What do you get for each?
when i removed item.name from the url and forced a value for name, i can get the whole list of objects. now, i need to find a way to get those values in my input and be able to actually search for something.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.