Undefined index error if any element is empty - arrays

I am sending 5 input values on single click in angularjs. Data is send as objects of object. For example
Object{0:Object, 1:object, 2:Object .., 5:Object}.
Every object have Two inputs say first name and last name. User can send any input pair.
I am sending this as post request. Now on Backend side, I m using Laravel. I m storing this object in a variable let say
`$x=$request->input('data');
I loop it for every object it will create new record.
Problem I am facing:
If all objects is sends then it properly saving the data in database.
But if one of the object is empty, Empty means if use enter 2nd input and 3rd input only and leave blank the 1st, 3rd and 5th input.
Then laravel throwing error of undefined index as on first object it not receiving any firstname or lastname for 1st request. SO how can I solve this problem.I wanted that it should update the fields whichever it receives.

It seems you need to filter out empty objects before persisting.
$data = $request->input('data');
$dataToSave = [];
foreach ($data as $value) {
if (!empty($value)) {
$dataToSave[] = $data;
}
}
// persist $dataToSave with your logic
Its better to change your firstname and lastname to nullable in order to accept null values

check the input values in ajax function and pass those key values only

Related

How can I use expressions to reduce array of objects

I am in the process of creating a flow which, upon the trigger activating, sends a prompt to a user.
The user selects a response, and then the flow sends a message in the appropriate channel. The prompt items come from the list channels action. I can use body('ListChannels')?['value] to parse the response. The response is an array with each object looking like:
{
"id":"",
"displayName":"General",
"description":"Used to test in-development features",
"email":"",
"webUrl":""
}
I want to reduce each object to just the displayName key and use the resulting as the inputs for the next action, this is the accepted input for that action:
["General", "Channel2", "Channel3"]
Is there a way to convert this using the provided expressions/actions for power automate? I also need to use the response to lookup the corresponding array entry "id" for sending a message to that channel.
One possibility is to use looping and control statements to perform 'reduce' and 'lookup' actions:
For reduce: Initialize variable(s) (array) first and push the lookup value to the array(s):
Since the input for the next action requires options that are readable to the user we have to use the channel name.
For lookup: Loop through the original object array and compare each object channel name to your query value:
The user response is stored in selectedResponse. We can loop through each item from the original list channels response and compare this to each objects channel name. If there is a match. We can use the current items id as the argument to the "Post your own adaptive card" action.
Result:

How do I use the last value in an array as a path for .child() when retrieving a snapshot?

I'm new to Firebase and have a function that writes all of my event ID's to an array. I want to use the last value in that array (the last event ID) to lookup the children of that specific eventID.
I know how to get the last item in the array but how do I put that into my .child() path?
I tried the code below, but it doesn't seem to work. I'm guessing that because .child("(lastEvent)") isn't a valid path.
let lastEvent = eventIDArray.last
refHandle = ref.child("Bouts").child("\(lastEvent)")
How do I plug the lastEvent value in as my path? Or is that even possible? Again, total newbie- alternatives welcome.
Sorting and filtering data
you can use sorting and filtering function to get the item.
To get the last item you can write the query like this.**
let recentBoutsQuery = (ref?.child("Bouts").queryLimited(toLast: 1))!
This will return 1 entry from last of your database which the last entry.
You can learn more from the firebase documentation. Work with Lists of Data

Firebase get value out of array

I'm making a realtime app with AngularJS and Firebase. I'm storing an array of data (players).
Below you can see a sample of my datastructure.
I can get all the players of this particular game, but how do I get for instance the player 'test'? The key is a generated value:
Game.find(gameId).child('/players/').push(player);
Then I thought, why don't I just 'update' '/players/'+name and set the name there. But unfortunatly I'm getting back objects inside an object:
{
players : {
"name":"...",
"test":"moretest"
}
}
I can access these properties but I cannot loop over them because it's not an array.
So basically what I want to do is "linq-wise" getting a player out of an array.
Game.find(gameId).child('/players/').where( name == ...);
Thanks in advance
Edit:
You are on the right track with storing just the name of the player instead of using push to generate an ID. You'll need to make sure your player names are unique though, otherwise you risk not being able to accommodate two players with the same name.
Every key in Firebase must have a value, so the best way to store a list of players where you want them to be accessible by the player name is to simply set them to a boolean value like 'true':
Game.find(gameId).child('/players').child(player).set(true);

Delete all invalid URLs of an array

I would like to delete all invalid URLs of an array. (e.g. v.1.1, version1.2.00, the.area.0a)
var myArray:Array = new Array("v.1.1", "example.com", "www.example.com","http://example.com","version1.2.00","http://www.example.com","the.area.0a","http://example.com/helloworld123");
How to do that?
I think there are 2 parts of the problem. One is that you want to filter your array. It would create a new array, but you can assign the return from filter back to myArray. The function that you pass to filter function can use String.match to match whatever type of regular expression you deem fit for a valid url (this matching is the second part of the problem).

In Meteor Collections, use an array-field or another collection?

The question
In short, my question is: when an array in a document is changed, will the users receive the new array, or just the changes?
If that question is unclear, I've described my problem below.
The problem
I have a collection whose documents contain an array field two users will push values to. A document in this collection kind of looks like this:
var document = {
userId1: "...user id...", // The id of the first of the two users.
userId2: "...user id...", // The id of the second of the two users.
data: [] // The field the two users will push values to.
}
data will from the beginning be empty, and the users will then take turns pushing values to it.
When one of the user pushes some value to data, the server will send the changes to the second user. Will the second user receive the entire data-array, or just the changes (the pushed value)? I'm a little bit worried that the second user will receive the entire data-array, even though it's just a single value that's been pushed to it, and if data contains many values, I fear this will become a bottleneck.
Is this the case? If it is, using another collection for storing the values will solve it, right? Something like this:
var document = {
id: "...unique id...",
userId1: "...user id...", // The id of the first of the two users.
userId2: "...user id..." // The id of the second of the two users.
}
var documentData = {
idReference: "...the unique id in the document above...",
value: "...a value..."
}
Instead of pushing the values into an array in document, insert them into a collection containing documentData. This (I know) won't have the downside I fear the first solution has (but I rather use the first solution if it doesn't have the downside).
As per https://github.com/meteor/meteor/blob/master/packages/livedata/DDP.md
changed (server -> client):
collection: string (collection name)
id: string (document ID)
fields: optional object with EJSON values
cleared: optional array of strings (field names to delete)
Users will receive the new array. To only send "diffs," use a collection of {userId: userId, value: value} documents.
I inspected what was sent as commented by user728291, and it seems like the entire array-field is sent, and not just the pushed value. I don't know if this always is the case (I just tested with an array containing few and small values; if it contains many or big values Meteor maybe try to do some optimization I couldn't see in my tiny test), but I'll go with the solution using another collection instead of the array-field.

Resources