I’ve got a tricky problem, but I’ve narrowed down what the problem is, but
I have no idea what the root cause is. I am trying to have my application
open up a number of asynchronous calls to a RESTful web service and then
re-calculate the average of the data as each request finishes. But my
problem is that the response is 0 at first and gets filled in later. I am
using the callback, so I know it’s not the problem of looking at the data
before the async method returns, but for some reason, the data being
returned by $resource is not matching what the web service returns.
Within my service, I have this code which creates an array of resource
calls, one for each merchantId.
> loadSystemMetrics: function (allMerchantData, $scope)
>
> {
>
> var index;
>
> for (index = 0; index < allMerchantData.length; ++index) {
>
> console.log(allMerchantData[index].Id);
>
>
> $log.info("Getting system metric for merchantId " +
> allMerchantData[index].Id);
>
>
> var metricResource =
> $resource('api/SystemMetrics/:merchantId', { merchantId:
> allMerchantData[index].Id });
>
>
>
>
> systemMetricResults[allMerchantData[index].Id] =
> metricResource.query(function (response) {
>
>
> $log.debug("systemMetricResults query returned:" +
> JSON.stringify(response));
>
> RecalculateSystemMetricSummary(response);
>
>
> // Emit the event that it changed, to force a client
> side refresh
>
> $scope.$broadcast('systemMetricSummaryChanged');
>
> });
>
>
>
> }
>
> }
>
Within RecalculateSystemMetricSummary have this:
> for (var merchantId in systemMetricResults) {
>
> // Collect the values, scores and errors to average out
>
> if (systemMetricResults[merchantId].$resolved) {
>
>
> systemMetricData =
> systemMetricResults[merchantId][index];
>
> metricCount += 1;
>
>
> $log.debug("systemMetricData.dataValue: " +
> systemMetricData.dataValue);
>
> metricValueSum += systemMetricData.dataValue;
>
>
> $log.debug("systemMetricData.dataScore: " +
> systemMetricData.dataScore);
>
> metricScoreSum += systemMetricData.dataScore;
>
> if (systemMetricData.dataScore.hasError) {
>
> // If any of them have an error, flag it
>
>
> $log.debug("systemMetricData.dataScore.hasError:
> true");
>
> systemMetricSummary[index].hasError = true;
>
> }
>
> }
>
> }
>
The problem I have is that even though a) I am waiting for the response and
b) I am checking that the response is $resolved, systemMetricData.dataValue
and systemMetricData.dataScore are 0. I’ve hardcoded the server to always
return some fixed values of 50 and 5,25 and 5, I’ve looked at Fiddler and
confirmed the data over the wire, added logging and watched through the
debugger. It should be greater than zero, but the response is never what I
expect. So my questions are:
1. How do you have code that executes when the data is returned.
Everything
I’ve read, this is the callback defined in query
2. Is there a better way to create an asynchronous list of resource
calls. I thought throwing them into an array would work.
I hope it’s not too confusing, but here is what my logging returns.
systemMetricResults query returned:[{"dataName":"Order
> Count","dataValue":50,"dataScore":5,"hasError":false},{"dataName":"Email
> List","dataValue":25,"dataScore":5,"hasError":false}]
>
> systemMetricData.dataValue: 0
>
> systemMetricData.dataScore: 0
>
> Setting metric value of 0 to 0.00 for 1 responses
>
> Setting metric score of 0 to 0 for 1 responses
>
> systemMetricData.dataValue: 0
>
> systemMetricData.dataScore: 0
>
> Setting metric value of 1 to 0.00 for 1 responses
>
> Setting metric score of 1 to 0 for 1 responses
>
> systemMetricResults query returned:[{"dataName":"Order
> Count","dataValue":50,"dataScore":5,"hasError":false},{"dataName":"Email
> List","dataValue":25,"dataScore":5,"hasError":false}]
>
> systemMetricData.dataValue: 0
>
> systemMetricData.dataScore: 0
>
> systemMetricData.dataValue: 50
>
> systemMetricData.dataScore: 5
>
> Setting metric value of 0 to 25.00 for 2 responses
>
> Setting metric score of 0 to 3 for 2 responses
>
> systemMetricData.dataValue: 0
>
> systemMetricData.dataScore: 0
>
> systemMetricData.dataValue: 25
>
> systemMetricData.dataScore: 5
>
> Setting metric value of 1 to 12.50 for 2 responses
>
> Setting metric score of 1 to 3 for 2 responses
>
> systemMetricResults query returned:[{"dataName":"Order
> Count","dataValue":50,"dataScore":5,"hasError":false},{"dataName":"Email
> List","dataValue":25,"dataScore":5,"hasError":false}]
>
> systemMetricData.dataValue: 25
>
> systemMetricData.dataScore: 3
>
> systemMetricData.dataValue: 50
>
> systemMetricData.dataScore: 5
>
> systemMetricData.dataValue: 50
>
> systemMetricData.dataScore: 5
>
> Setting metric value of 0 to 41.67 for 3 responses
>
> Setting metric score of 0 to 4 for 3 responses
>
> systemMetricData.dataValue: 12.50
>
> systemMetricData.dataScore: 3
>
> systemMetricData.dataValue: 25
>
> systemMetricData.dataScore: 5
>
> systemMetricData.dataValue: 25
>
> systemMetricData.dataScore: 5
>
> Setting metric value of 1 to 4.17 for 3 responses
>
> Setting metric score of 1 to 4 for 3 responses
>
I would probably consider myself a newbie with AngularJS. Lots of book
knowledge, but low on experience, so bear with me.
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.