Hi there, I'm using SpringDataRestAdapter to interact with my spring data rest api. From the screen shot you can see that I've just loaded all the questions and I'm attempting to load their answers (one-to-many) relationship. In the debug window the _embeddedItems array is an array of Questions with _links to each questions answers. I'm trying to prompt SpringDataRestAdapter to autoload the answers by passing 'answers' as an argument to the processWithPromise call but to no avail.
The urls look like
http://localhost:8080/api/questions
http://localhost:8080/api/question/1/answers
The Question.loadAnswers(question) call is commented out... It works in that it loads the question's answers but I felt I was using the framework incorrectly.
Here is the method
Question.loadAnswers = function (question) {
question.answers = question.answers? question.answers : [];
angular.forEach(question._links.answers, function (answerLink) {
var deferred = $http.get(answerLink);
return SpringDataRestAdapter.processWithPromise(deferred).then(function (data) {
question.answers = data._embeddedItems;
});
});
};
Thanks,
Mark.
please have a look at this answer on the corresponding Github issue here https://github.com/guylabs/angular-spring-data-rest/issues/22.
Thanks and regards,
Guy
Related
My application is kind of a poll application. So the admin asks a question, and users respond to that question, and every data is stored in Firestore. In the admin page, the admin can see the question that he/she created, and when the admin clicks the question, he/she can see the answers of users to this question.
On the answers page, I am trying to reach every user's uid but I couldn't achieve this. I can print them but I can't use them in a way. I think I make some mistakes with Firebase functions.
This is an example of a method that I am trying to return the list includes of the specific question. But this function doesn't return anything.
List getUserIDData() {
List<String> growableList = [];
List<String> userAnswerList = [];
String adminQuestion = "What is the reason of life?";
Firestore.instance.collection("user").getDocuments().then((snapshot) {
snapshot.documents.forEach((element) {
growableList.add(element.documentID);
});
for (int i = 0; i < growableList.length; i++) {
Firestore.instance
.collection("user")
.document(growableList[i])
.collection("answers")
.where("adminQuestion", isEqualTo: adminQuestion)
.getDocuments()
.then((snapshot) {
snapshot.documents.forEach((element) {
userAnswerList.add(element["userAnswer"]);
});
});
print(i);
}
});
return growableList;
}
I also tried something with StreamBuilder, but still, after Firestore.instance, everything is deleted automatically. In Firestore, everything looks good, but after it finished growableList doesn't stay the same and this function returns null.
Do you have any suggestions to do so?
From what I can see you are using a very old version of FlutterFire, for example the following has been changed:
BREAKING: getDocuments/get has been updated to accept an instance of GetOptions (see below).
DEPRECATED: documents has been deprecated in favor of docs.
DEPRECATED: documentID has been deprecated in favor of id.
You might want to migrate to a newer version.
With snapshot.documents you get a List<QueryDocumentSnapshot<T>> according to the documentation. So I looked at the QueryDocumentSnapshot and was surprised that you apply element["userAnswer"] directly to it. According to the documentation you have to use element.data() to get the data.
If that doesn't help I would need more information in the form of output what's behind the element variables.
I'm using angular-fullstack to build a single page application, and in one of my controllers I'm trying to assign a user's id to a variable. The code
$scope.getCurrentUser = Auth.getCurrentUser;
returns
function () {
return currentUser;
}
This works fine for displaying in my view as my angular code can interpret the function and display the user id using {{getCurrentUser()._id}} which I'm assuming evaluates the promise and displays the code to the view.
My question is how do I assign $scope.getCurrentUser = Auth.getCurrentUser; to a variable in my controller? Whenever I do so, I get undefined variables. I've tried:
$scope.getId = Auth.getCurrentUser()._id;
console.log($scope.getId); //undefined
$scope.getId = Auth.getCurrentUser();
console.log($scope.getId._id); //undefined
I've read forum posts like this and this that explain that these methods are meant to be returned the way they are. Another post that is basically the same question as mine, but I'm still confused how to store the user id becuase the answer was hinting it was console.log that was the issue. Any help would be greatly appreciated, thanks.
Try the following and let me know how this works out:
angular.module('yourModuleName').controller('YourController', ['$scope', 'Authentication',
function($scope, Authentication) {
var authentication = Authentication;
$scope.getId = authentication.user._id;
console.log($scope.getId);
}
]);
Make sure Authentication is included in your controller.
I'm having a slight issue with my ability to consume REST data retrieved via Restangular in an angular controller. I have the following code which works fine for a list of accounts:
var baseAccounts = Restangular.all('accounts');
baseAccounts.getList().then(function(accounts) {
$scope.accounts = accounts;
});
This works perfectly for a list. I use similar syntax for a single account:
var baseAccount = Restangular.one('accounts');
baseAccount.getList(GUID).then(function(returnedAccount) {
$scope.currentAccount = returnedAccount;
});
I am using ng-repeat as the handling directive for my first request. I am attempting to bind with {{ account.name }} tags for the single request, but it does not seem to display any data despite the request being made properly. GUID is the parameter I must pass in to retrieve the relevant record.
I have combed through Restangular docs and it seems to me like I am composing my request properly. Any insight would be greatly appreciated.
EDIT: I've tried all of the solutions listed here to no avail. It would seem Restangular is submitting the correctly structured request, but when it returns it through my controller it shows up as just a request for a list of accounts. When the response is logged, it shows the same response as would be expected for a list of accounts. I do not believe this is a scoping issue as I have encapsulated my request in a way that should work to mitigate that. So, there seems to be a disconnect between Request -> Restangular object/promise that populates the request -> data-binding to the request. Restangular alternates between returning the array of accounts or undefined.
Have you looked at:
https://github.com/mgonto/restangular#using-values-directly-in-templates
Since Angular 1.2, Promise unwrapping in templates has been disabled by default and will be deprecated soon.
Try:
$scope.accounts = baseAccounts.getList().$object;
try:
var baseAccount = Restangular.one('accounts', GUID);
baseAccount.get().then(function(returnedAccount) {
$scope.currentAccount = returnedAccount;
});
The problem here is that it's expecting an array to be returned. I'm assuming that you are expecting an account object. Thus we need to use the get function, intead of getList()
The one() function has a second argument that accepts an id e.g. .one('users', 1). You can take a use of it.
CODE
var baseAccount = Restangular.one('accounts', 1); //1 would be account id
baseAccount.getList('account').then(function(returnedAccount) {
$scope.currentAccount = returnedAccount;
});
OR
var baseAccount = Restangular.one('accounts', 1); //1 would be account id
baseAccount.all('account').getList().then(function(returnedAccount) {
$scope.currentAccount = returnedAccount;
});
For more info take look at github issue
Hope this could help you, Thanks.
I have been through several tutorials and posts about this topic and still can't seem to figure out what is wrong with my code. To me it seems I am having scoping issues with the data within my service. My code is split up into separate files. Here is my code:
github link : https://github.com/StudentJoeyJMStudios/PetPinterest.git
//in dataService.js
var app = angular.module('se165PetPinterestWebApp');
app.service('SharedData', function ()
{
var categoryOfSelectedAnimals = [];
this.setCatOfSelAnimals = function(pulledCategoriesFromParse)
{
categoryOfSelectedAnimals = pulledCategoriesFromParse;
console.log('after assignment in set::' + categoryOfSelectedAnimals);
};
this.getCatOfSelAnimals = function()
{
console.log('in get::::' + categoryOfSelectedAnimals);
return categoryOfSelectedAnimals;
};
});
in my first controller to set the data in signup.js
app.controller('SignupCtrl',['$scope', 'SharedData', function ($scope, SharedData)
{
var Categories = Parse.Object.extend('Categories');
var query = new Parse.Query(Categories);
query.find({
success: function(results)
{
$scope.availableCategoriesOfAnimals = results;
SharedData.setCatOfSelAnimals(results);
},
error: function(error)
{
alert('Error: ' + error.code + ' ' + error.message);
}
});
};
}]);
Then in my other controller trying to get the data from the array within my service:
var app = angular.module('se165PetPinterestWebApp');
app.controller('CatSelCtrl', function ($scope, SharedData)
{
$scope.availableCategoriesOfAnimals = SharedData.getCatOfSelAnimals();
});
When I print the contents from the SharedData.getCatOfSelAnimals I get 0 every time. Please help. Thank you very much in advance.
EDIT: After playing around with a string a bit I am finding the changed string in the set function is not saved into the service and when I call my get function within my service the string is not changed from the set method. Please help, thank you in advance.
EDIT: So it looks like when I navigate to new page by using window.location.href = '../views/categorySelection.html'; in my signup.js it reloads my dataService.js which re-sets my variables back to nothing. Does anyone have any ideas as to how to fix this?
Edit
First: why you lose data
You need to setup routing properly. Right now you are not changing views but rather using window.location.href to load a new bootstrap file (dashboard.html), i.e. everything saved in memory will be lost. So you have been doing it right, sort of, but the moment you change to dashboard.html all data from Parse is lost.
You can solve this by configuring routes and use $location.url() to change URL. Read more about angular.route here: https://docs.angularjs.org/api/ngRoute/service/$route
The angular way
After looking at your code and running your app I think we need to take a step back. Angular is tricky to get used to but there is a lot of good tutorials. I think you might wanna read some of them to get a better grasp of how it works and how to setup and build your app.
Start here: http://www.airpair.com/angularjs
Boilerplate
Found this boilerplate for an Angular app using Parse. It might be something you could use. https://github.com/brandid/parse-angular-demo
Original
Or an even quicker way to empty $scope.availableCategoriesOfAnimals and then merge new data without breaking reference:
$scope.availableCategoriesOfAnimals.length = 0;
Array.prototype.push.apply($scope.availableCategoriesOfAnimals, pulledCategoriesFromParse);
You are breaking the reference on assignment. This is a JavaScript issue, not an angular one for that matter ;)
Try this in your set function:
categoryOfSelectedAnimals.length=0;
pulledCategoriesFromParse.forEach(function (e) {categoryOfSelectedAnimals.push(e)});
in stead of reassigning
edit: angular extend works on objects, not arrays, so replaced it with a bit of JS.
I'm using CamanJS to add some image processing features to my website, it's a simple and great library but still its documentation is somehow poor.
I'm uploading an image to my website, then I'm applying all the effects on the image uploaded (it's not saved on the Server, I'm modifying it on the client side).
as shown on the official website (http://camanjs.com/guides/#BasicUsage):
function invertColors() {
Caman("#original-img", function () {
this.invert().render();
});
}
The problem is when I re-upload a new image. apparently CamanJS keeps the first image cashed, and the new image is not shown.
when I read about this issue the only place I found an answer for this is here:
CamanJS - change underlying canvas after applying filters/manipulations
but I'm sorry the answer was not that clear for me. so I have to ask it again.
the answer suggested to use reloadCanvasData() but I didn't know exactly how to use it, I tried so many ways but all went in vain!
I tried:
Caman("#original-img", function () {
this.reloadCanvasData();
});
and:
Caman.reloadCanvasData();
etc.
Can anyone provide a working example?
Thanks
I thought I'd help those who came here looking at how to replace PIXEL data rather than just a loaded image:
can1 = document.getElementById("MyCanvas1");
ctx1 = can1.getContext("2d");
var c = <do what ever you need and make a new canvas here>
ctx1.putImageData(c, 0,0); // <---this replaces the pixeldata
Caman("#MyCanvas1", function () {
this.render();
});
This way you can process the image at the pixel level, and then get it back into camanjs.
As a solution what I have done is clear the canvas and have again Inserted an HTML Image tag. before calling Second Image.with camanjs
something like following
function clearCanvas() {
$('#ImageParentDiv').empty();
var htmlTag = '<img src="../images/Loading.gif" id="original-img">';
$('#ImageParentDiv').html(htmlTag);
}
Just call the revert() when you need an original image:
Caman("#original-img", function () {
this.revert();
this.render();
});