I'm using a REST server to retrieve data. In the AngularJS UI the user is given the choice of a few options to create a query to send to the REST server. The problem is the server only accepts one of each, so if user wants to search for multiple Entities, they can't. I'm trying to think of a way to send off multiple requests (quantity of requests depends on length of Entity array, which is set by the user in UI). So far all I can think of is for loop by the length of the entity array, and for each loop send a request - my problem is how do I join these two sets of data? Each time the for loop completes the data is overridden with the next set it's sent off for. And the amount of times the requests are sent is totally dependent on the amount of entities the user needs returned.
If you have any unique identifier for each result item then you can try the following algorithm. Hopefully It will solve the issue.
var data = [];
loop through options selected by user {
request sent {
on sucess() {
loop though RESPONSE_DATA_ARRAY {
var id = RESPONSE_DATA_ARRAY_ITEM.uniuqe_key
if(data[id] === undefined){
data[id] = RESPONSE_DATA_ARRAY_ITEM;
//Stored as Key Value pair, which will help to identify same object each time easily.
}
}
}
}
}
Related
In my app users send a form to the server, I'm retrieving the last one sent in the next way:
const property = set[set?.length -1 ]
But I would like to retrieve not the last form sent in general but the last one sent by the user using the app. I was trying with:
if (user._id === set?.owner)
But if the userID isn't the same that the one who sent the last form it won't return anything and without the condition it just show the last form sent independently of the user accesing it.
Filter the array to find possible valid matches first.
const setByUser = set.filter(form => form.owner === user._id);
Then get whichever one you want from that array.
If I have a database structure like here and I make a query as shown below.Is there a difference on the traffic used to download the snapshot from the database if I access each node with snapshot.forEach(function(childSnapshot) and if I don't access the nodes?
If there is no difference, is there a way to access only the keys in Chats without getting a snapshot data for what each key contains.I'm assuming that this way it will generate less downloaded data
var requests = db.ref("Chats");
requests.on('child_added', function(snapshot) {
var communicationId = snapshot.key;
console.log("Chat id = " + communicationId);
getMessageInfo(
communicationId,
function() {
snapshot.ref.remove();
}
);
When you call requests.on('child_added', ...), you are always going to access all of the data at the requests node. It doesn't matter what you do in the callback function. The entire node is loaded into memory, and cost of the query is paid. What you do with the snapshot in memory doesn't cost anything else.
If you don't want all of the child nodes under requests, you should find some way to filter the query for only the children you need.
As they mentioned in the documentation, either of these methods can be used:
Call a method to get the data.
Set a listener to receive data-change
events.
Traffic depends upon our usage. When your data need not get updated in realtime, you can just call a method to get the data (1) But if you want your data to be updated in realtime, then you should go for (2). When you set a listener, Firebase sends your listener an initial snapshot of the data, and then another snapshot each time the child changes.
(1) - Example
firebase.database().ref('/users/').once('value') // Single Call
(2) - Example
firebase.database().ref('/users/').on('child_added') // Every Update It is Called
And also, I think you cannot get all keys, because when you reference a child and retrieve a data, firebase itself sends it as key-value pairs (DataSnapshot).
Further Reference: https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot
https://firebase.google.com/docs/database/web/read-and-write
I have a piece of functionality in my Angular app in which I have some searchable items that exist within my Algolia database. The thing is...they aren't searchable by any string. They are only searchable via facets.
The problem is that when I run my initial .search() function with an empty string + my search filters/facets, I get a list returned and everything is all fine and dandy. HOWEVER, when I go to run the function again to refresh the list, it just comes back with the same results and actually never fires a new request, unless I change one of the filters/facets.
Is there any way to force a search query any time I want, without having to specify a "new" search criteria?
Here is my search function:
searchForAuditions() {
// Setup the filters/options
let options = {
highlightPreTag: '<span class="highlighted">',
highlightPostTag: '</span>',
hitsPerPage: 10,
};
// Add clause to make sure "is_deleted" is false
let isDeleted = ` is_deleted: "false"`;
let facets = `${isDeleted}`;
// Replace all trailing spaces and split it into an array
let facetWords = facets.replace(/\s+$/, '').split(" ");
// Remove trailing "AND"
if((facetWords[facetWords.length - 1] === "AND") || (facetWords[facetWords.length - 1] === "OR")) {
facetWords.splice(-1, 1);
facets = facetWords.join(' ');
}
if(facets) {
options['filters'] = facets;
}
this.auditionsIndex.search('', options).then(result => {
this.results = result.hits;
})
}
Thanks in advance!
From the algolia JavaScript client documentation,
To avoid performing the same API calls twice search results will be stored in a cache that will be tied to your JavaScript client and index objects. Whenever a call for a specific query (and filters) is made, we store the results in a local cache. If you ever call the exact same query again, we read the results from the cache instead of doing an API call.
This is particularly useful when your users are deleting characters
from their current query, to avoid useless API calls. Because it is
stored as a simple JavaScript object in memory, the cache is
automatically reset whenever you reload the page.
To resolve this you should use
index.clearCache() or client.clearCache()
Read more about the inbuilt cache system here.
https://github.com/algolia/algoliasearch-client-javascript#cache
I have a table called Roster where it stores a User Pointer in one of the column. I am trying set that column in before save method when it is new.So far i have this but i am not sure how to get the user id
Parse.Cloud.beforeSave("Roster",function(request,response){
//Update roster with sender Id
if (request.object.isNew()){
var userPointer = /*NOT SURE HOW TO GET WHO SENT IT*/
request.object.set("User",userPointer);
}
response.success();
});
Any idea?
var userPointer = request.user;
Is the proper method. Todd's answer works on the client side, but not on cloud code, where beforeSave triggers occur.
If you need to access any of the user's information beyond their id, you'll have to first fetch the user, as the entire object is not sent in the request.
Edit - Just wanted to add that before/afterSave triggers have a 3 second timeout. This is enough time to perform a quick query or two, but if you have a lot of objects in your database, or perform many save/fetch/query calls, you may end up exceeding your 3 second limit. If you have a lot of that logic that needs to occur, rather than saving the object from the client, call a cloud code function that handles all of those changes, then saves the object and returns the newly saved object, so that you can set your client side object to the returned, up to date object.
As Jake T pointed out, you will need to use
var user = request.user
Parse.User.current() is not supported in a cloud code environment.
So I'm trying to go through one Firebase database to find entries in the database matching a criteria. Therefore I'm using the deferred object of jQuery to handle the database calls.
Once I get a return value from this first database I want to get the user info from a second database for each of those values in the first db. Then the results are added to a JSON array
so its:
<search for value, find one>
<<<search other db for oher info>>>
<continue search for outer value>
But this only returns one value - although everything else is running fine (and the console logs all the info correct).
Here's the code:
function find(searchLocation, profileID) {
var requestUserData = {
data: []
};
var def = $.Deferred();
//This will be executed as long as there are elements in the database that match the criteria and that haven't been loaded yet (so it's a simple loop)
Ref.orderByChild("location").equalTo(searchLocation).on("child_added", function(snapshot) {
def.ressolve(snapshot.val().ID);
});
return def.promise();
};
I hope you guys have any ideas on what to do or how I could solve this. Thanks in advance!
Edit: upon further testing I discovered that this problem already exists in the outer loop - so only the first value is being returned. I think this is related to the posission of the resolve() method but I didn't find a posibility on how to change this behaviour.
Firebase is a real-time database. The events stream as changes occur at the server. You're attempting to take this real-time model and force it into CRUD strategy and do a GET operation on the data. A better solution would be to simply update the values in real-time as they are modified.
See AngularFire, ReactFire, or BackboneFire for an example of how you can do this with your favorite bindings framework.
To directly answer the question, if you want to retrieve a static snapshot of the data, you want to use once() callback with a value event, not a real-time stream from child_added:
Ref.orderByChild("location").equalTo(searchLocation).once("value", function(snapshot) {
def.resolve(snapshot.val());
});