0

After making a call to rest api I am getting this data in javascript.

{
  "error": false,
  "orders": [
    {
      "oid": "764",
      "cid": "423",
      "name": "Akshay jain",
      "address": "infront of gwal magra talab",
      "mobile": "11111111",
      "email": "[email protected]",
      "odate": "2016-03-28 21:50:45",
      "status": "NEW ORDER",
      "payment": "1",
      "ddate": "2016-03-28",
      "dtime": "1"
    },
    {
      "oid": "763",
      "cid": "438",
      "name": "Vishwakarma Ji",
      "address": "Narayan Pura Road",
      "mobile": "0000000000",
      "email": null,
      "odate": "2016-03-28 20:02:06",
      "status": "Confirmed (Ready for Delivery)",
      "payment": "1",
      "ddate": "2016-03-28",
      "dtime": null
    }
  ]
}

This is how i am storing in controller.

$scope.result = Order.query(); // where Order is a service in angularjs

If i am trying to print $scope.result.error it is giving object object not the actual error value.

How to parse error and orders in javascript and save it in a variable?

1
  • var data = JSON.parse('server.response'); Commented Mar 29, 2016 at 1:54

2 Answers 2

2

If this Order.Query is async, you have to set the result in a callback

app.controller("OrderIndexCtrl",function($scope,Order) {
  Order.query(function(data){
     if(data.error == false) {
        $scope.result = data.orders;
     }
     else {
        ...
     }
  });
});
Sign up to request clarification or add additional context in comments.

Comments

0

Try using the following code.

$scope.result = JSON.parse(Order.query());

Source: this question

2 Comments

Thanks for your response. But after JSON.parse it is giving error Unexpected token o at Object.parse
Can you please update the question with the Order.query() service?

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.