Access the last document relationing it with an user - javascript

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.

Related

How to get value from firebase without the snapshot key [JS]

Im trying to make friends system but in invite accept i have to delete invite of a guy in his list but i cant do it without the key.
How do i get the snapshot key by value (in this case uid) in database?
Firebase Realtime Database queries work on single path, and then order/filter on a value at a fixed path under each direct child node. In your case, if you know whether the user is active or pending, you can find a specific value with:
const ref = firebase.database().ref("friends");
const query = ref.child("active").orderByValue().equalTo("JrvFaTDGV6TnkZq6uGMNICxwGwo2")
const results = query.get();
results.forEach((snapshot) => {
console.log(`Found value ${snapshot.val()} under key ${snapshot.key}`)
})
There is no way to perform the same query across both active and pending nodes at the same time, so you will either have to perform a separate query for each of those, or change your data model to have a single flat list of users. In that case you'll likely store the status and UID for each user as a child property, and use orderByChild("uid").
Also note that using push IDs as keys seems an antipattern here, as my guess is that each UID should only have one status. A better data model for this is:
friends: {
"JrvFaTDGV6TnkZq6uGMNICxwGwo2": {
status: "active"
}
}

Add multiple values to the URL dynamically

I am trying to make a request to an endpoint that expects only one user account Id, so I tried to separate the user account Ids (if they are multiple) by &. The this.account is an array with account Id strings. And this is how I am doing it but it still appends the values with a comma:
getAccountStats(callback) {
let acc = this.account.map((val) => {
if (val>1) {
return 'accountID_'+val+'&'
}
return 'accountID__'+val;
})
let url = `/${acc}/userAccount`;
axios.get(url, callback);
}
When I console.log the url, it returns /accountID_1,accountID_2/userAccount but I want it to be /accountID_1&accountID_2/userAccount. Any idea how to achieve this. TIA
I am trying to make a request to an endpoint that expects only one user account Id, so I tried to separate the user account Ids (if they are multiple) by &
The short answer is what you think you want won't ever work. If the endpoint only expects 1 account id, then trying to add more won't do what you want.
but I want it to be /accountID_1&accountID_2/userAccount
This doesn't look like a valid endpoint. So I doubt you really want this.

How to retrieve data from Firebase in Javascript?

I have one node named as group_chat and another one named as users. The node group_chat contains the children which contains message, time and sender_id. Whereas the node users contains the details of users. When I retrieve chats from group_chat node, I also want to display the information of each sender. On using this code
rootChat.on("child_added", function (snapshot) {
if(snapshot.val().sender_id == logged_in_user_id) {
// Message by You
// Print snapshot.val().message
}
else{
const refUser = "/users/"+snapshot.val().sender_id;
const rootUser = database.ref(refUser);
rootUser.once("value", function(snap) {
// Print snap.val().name
// Display snap.val().picture
// Print snapshot.val().message
});
}
});
The problem is, when the code goes in else condition where it's the message of the other user, it retrieves the information of the user by going through the specific node. But while doing it, it automatically goes to next child while going through the user detail of first child which automatically spoils the arrangement of messages showing latest message in the middle, middle one in end and so on.
If I have 4 children named as -child1, -child2, -child3 and -child4 in node group_chat, while checking the -child1, it will also check -child2 and sometimes it will print -child 2 before -child1.
How can I resolve it? Is there any way where I can wait for the else condition to finish and then go to next child? Or is there any way where I can get user detail in just one line of code somehow like this
// rootUser.child.name
How about pre-loading the users, before you start building the HTML?
The outline of that would be something like:
Use on("value" instead of on("child_added, so that you get all messages in one go.
Loop over the messages to determine the unique user IDs.
Load all users with once("value") calls. Note that once() returns a promise, so you can wait to all users to be loaded with Promise.all().
Now loop over all messages again to build the HTML.
At this point if you need user data, you can look it up without needing an asynchronous call.

Merge $http.get JSON data in for loop

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.
}
}
}
}
}

URL Requested too long in querystring

I'm passing data on a page1 through a querystring onto page2, when a user clicks the button "send" the data is passed on the querystring and page2 gets the 'data=..' parameter from the querystring and shows it's value on the page.
The problem is that the data on page1 is created through the user by the input textbox and can be quite long. This gives us the following error when the user clicks "send"
URL Requested is too long
This is the code used to get the span element(submitted text by user) and convert it to a variable which is added onto the querylink:
$('#send').click(function() {
var data_text = $('span').prop('outerHTML');
window.location.href = 'http://swter.com/send.php?data=' + data_text + '';
return false;
});
Are there anyways around it apart from limiting the amount of chars a user can type?
So you could split the contents of the textarea into multiple strings using String#split and then loop through the resulting array and make AJAX GET requests to your back end server. You will need to include a form of unique identifier that ties each batch of data together on the server, and an index so you can rebuild it, ie:
?id={{unique_id}}&page=1&total=6&body={{message_page_1_of_6}}
However, as pointed out, a POST request would be more appropriate here.

Categories

Resources