Trying to use two middle ware passing them as array - middleware

app.use([logger,authorize])
Error:
TypeError: Router.use() requires a middleware function but got a Object

You usually don't pass them as an array. You pass them as a comma seperated list as denoted by https://expressjs.com/en/4x/api.html#app.METHOD.
This would then be
app.use(logger,authorize)
However if you would like to call them as an array, take a look at function.apply

Related

dart removeWhere() - where does the data go?

I am trying to write a function in dart that takes a string that has some words that be begin with a "#" character and split it into two sets. One that contains string with only the "#" character and one with no "#" characters.
There are some obvious ways i could do this with a for loop, but want to use these fancy list processing functions (what are these called exactly?) like where() forEach().
so here is my attempt.
void main() {
String inputText = "Hello world. #hithere";
inputText.split(" ").where((element) => element.startsWith("#"))
.forEach((item) {
print(item);
});
inputText.split(" ").removeWhere((element) => element.startsWith("#"));
print(inputText);
}
Here is the output of the program:
#hithere
Hello world. #hithere
the first part works as expected, but the second part i expected the removeWhere would remove the element #hithere from my list....
but this this didnt happen.
So my question is this. Did the removeWhere() method remove this element correctly? If so where is the list that has this element removed? How do i get access to it?
my explanation of what happend is that the split(" ") method created a List and the removeWhere() method operated on this List and removed the element i told it to... but what i dont understand is how i can get access to this list? where does this list exist?
what i was think i should do is something like this:
inputText.split(" ").removeWhere((element) => element.startsWith("#")).join();
I was hoping that i could take the List with the removed elements and join() them back into a string... but since the removeWhere() method returns void.. i have no idea where the list that has removed elements exists?

Uri class throws error when queryParameters contains a key with a value: false?

I was working through some code, and noticed:
return new Uri(host: server, path: apiPath, query: query, queryParameters: queryParams);
This code is executed regularly throughout the application, and the only difference was queryParams. So i printed it out:
{Id:[1234], enabled:false}
shows it is a key:value set of: Id:List, enabled:boolean.
The stack trace i get is:
which shows the map and then the trace. #6 points to the above line.
It is looking at false... something with iterating false is what breaks this.
When dealing with the URI and query parameters, it is looking for numerics, lists, and strings but not booleans. In order to resolve this and allow it to function correctly, you will need to do:
{"enabled": false.toString()}
// or
{"enabled": "false"}
and the uri class will set the query parameter accordingly.
The Uri class is located in core library for Dart. When we are using it, we are passing in the created Uri object into an action for a client class,
Client client = new BrowserClient();
which accepts the url as a part of the parameters.
While looking at the errors above though, the Uri class ultimately is unable to properly parse a false value to an accepted value.
When looking at the Code Docs for Uri as per the Dart languages: https://api.dartlang.org/dev/1.25.0-dev.7.0/dart-core/Uri/Uri.html
The query component is set through either query or queryParameters. When query is used, the provided string should be a valid URI query, but invalid characters, other than general delimiters, will be escaped if necessary. When queryParameters is used the query is built from the provided map. Each key and value in the map is percent-encoded and joined using equal and ampersand characters. A value in the map must be either a string, or an Iterable of strings, where the latter corresponds to multiple values for the same key.
Which makes sense to say all values must be String or an Iterable of Strings. The only thing which I cant figure out is that in Dartpad, true and false have toString functions, and yet you can also pass numerics in there.
The only conclusion is that while it accepts Strings and Iterables of Strings, it will also parse ints and other numerics because they will explicitly check for that type as it is common to see in URI.
One would think that the URI would understand booleans since those are also common place, but that is yet to be seen since I cant take an explicit look at the source code for dartlang. I did however manage to look at the source code for it and narrowed it down. writeComponent points to _Uri._uriEncode but when looking at that function, there is no code as much as just a definition.
HTH.

Swift: How do I store an array of object by reference from completion handler (closure)?

There's a API callback returns a Json format result. The requirement in short is that I need to keep calling this API and keeps implement Breadth First Search on the results it returns.
Imaging it's a map with many nodes and connections. Every time I call API call for a node, it gonna return me list of its connected nodes. All I need now, is an array, which saves all the node that has been visited to avoid repeated visits.
But this is Swift and I am new to it. I was using Array and pass as inout inside the completion handler. But there's an error: escaping closures can only capture inout parameters explicitly by value which means I cannot do it like this.
Now you may ask why the array I have has to be stored by reference. Because the API call is async, which means I have to wait until it comes back to keep progressing Breadth First Search, means I have to pass this array by reference in order to do the recursion.
What other solutions I may have?
Swift Arrays are value types (not reference types) so you will need to store you array in an object. You can then pass the object to your handler and set the array content inside the object which is carried as a reference in the closures.

NSMutable array containsObject, does it check the contents of the object or the object type?

I am a little confused to how containsObject works. Does it check to see if it contains an instance of an object type or does it compare the inside of the objects variables etc to see if they match?
This is an implementation detail, you can work on the basis that it calls isEqual: on each items and works on the result of that.
Under the hood it's probably calling hash on each item and comparing that, then, if the hash matches it will call isEqual: to make sure it's a real match.

Ruby on Rails Array access

My issue involves an array that is apparently not nil but when I try to access it, it is.
The array is returned from a find on an active record. I have confirmed it is in fact an array with the .class method
#show_times = Showing.find_showtimes(params[:id])
#show_times.inspect =>shows that the array is not empty and is a multidimensional array.
#show_times[0] =>is nil
#show_times[1] =>is nil
#show_times.first.inspect => NoMethodError (undefined method `first')
I am perplexed as to why this is..
It's not an array, it's an active record magic structure that mostly looks like an array. I think you can do to_a on it to get a real array, but then a lot of the magic goes away.
find_showtimes is not a built-in finder - if it were, it would return either a single ActiveRecord object or an array of ActiveRecord objects - never a multidimensional array. So the first thing I'd do is have a look a the source of that method.
EDIT: Given your source code (in the comment) - you're actually getting a Hash, not an Array, back from your finder. See the docs for Enumerable's group_by. The Hash is mapping your theater objects to an array of showing objects. This is what you want to do:
Showing.find_showtimes(params[:id]).each do |theater, showtimes|
puts "Theater #{theater} is showing the movie at #{showtimes.join(', ')}"
end
If you're in the console and you want to explore the #show_times object, you can use the keys method to see all the hash keys and then get to individual hash items (such as all the show times) like so:
#show_times[:theater_one]
Same thing will work in your model/view but using each/map/collect would be cleaner as pretty code goes.

Resources