In my REST API Controller, I receive a list of strings, if the input list is empty i should return bad request.
The problem is the input is empty, the list contains no items, but the check:
if(productIdsList.isEmpty)
Return false.
How could that be ??
It is not empty, it contains an empty String.
Seems like Eclipse shows and empty String as an empty value (and not as "" in Scala REPL) and this is confusing.
Try debugging this, it looks exactly the same.
object A extends Application {
val a = List("")
//any other code here
}
The empty list is Nil and it looks exactly like productIdsList.tl in your debug view
Related
I have a list that I am getting the values from an API,
it is List<dynamic> type, while I print it I am getting this output (for example): [cat, female], but when I use inspect it has three values: "cat", "female", "". The last empty value is making some problems in my code, so I wanted to remove it, but I don't know how to do this.
As it is a List<dynamic> I used removeLast() and also toString() but none of them worked for me. I appreciate any help on this.
The solution is to filter items and get the new list:
final newList = list.where((e) => e != null && e != '').toList();
removeLast() does not work as you expected. If you read the comment of the method it says Removes and returns the last object in this list.
PS: I recommend you use a functional way to deal with a list which means do not modify the state of the original list instead, get a new list.
I have an array of words, some may or may not have typos.
potentialWords = ["hello", "lkasjdf", "hunry"]
What I want to do is, return an array of all valid words, and also those words that were able to be autocorrected using a function I created correctWord. It returns an array of potential matches. so "hunry" might return ["hungry", "hurry"]. I will select the first index for the best guess.
Some words cannot be corrected however! e.g. "lkasjdf" will not find any corrections, but "hunry" will.
I was trying something like:
potentialWords.map {
if correctWord($0) != nil {
return correctWord($0)[0]
}
}
of course this will complain and say that I need a return outside the if clause. I can filter the list based on if the word can be corrected, and then map over the filtered list, re-checking which words need to be corrected, but this runs the correctWord function way too many times, and it is very sensitive.
I would like to be able to do one single pass through, and return an array of all valid words, and also corrected words.
P.S. I am calling correctWord twice in the map function for brevity, but of course I would assign correctWord($0) to a variable, and then if it isn't nil, take the first index and add it to the new list.
I think you're after flatMap. It's the same as map except it will also filter out any nil values.
potentialWords.flatMap { correctWord($0)?.first }
I'm getting a response in JSON format, which contains an _id that is stored as an ObjectID in Mongodb on the server side. However, I change it into a String, and it still won't let me add it. Is it because it has numbers? I need the element to be identifiable by the id, so if I can't append this way, is there any other way I can reference the element by the id?
var group = d3.select("#containerthing");
var id = response._id.toString();
console.log(id);
//5802bc044f6313c1097de4a2
var responseNode = group.append(id).attr("fill","black").attr("x", 15).attr("y", 15).attr("width", 190).attr("height", 90);
//InvalidCharacterError: String contains an invalid character
I believe that I understand your problem.
D3's .append():
If the specified type is a string, appends a new element of this type (tag name) as the last child of each selected element, or the next following sibling in the update selection if this is an enter selection. [...] This function should return an element to be appended. (The function typically creates a new element, but it may instead return an existing element.
Why .append() work fine if you pass 'foo'? Because D3 append a custom tag element. If you see in your console I'am sure that you will see <foo>...</foo>
Why .append() work wrong if you pass '5802bc044f6313c1097de4a2'? A custom tag element can't start with a number. You don't use _id, you should try to find another pattern for identify your element.
I hope that helps
The reason was that you can't start elements with numbers. I had to do:
var responseNode = group.append("n"+id).attr("fill","black").attr("x", 15).attr("y", 15).attr("width", 190).attr("height", 90);
to get it to work.
I stumbled upon a weird thing when trying to fetch an object from my Realm (iOS, Swift, Realm version 0.98.2)
print("speaker:")
print(RealmProvider.appRealm.objects(FavoriteSpeaker).first!)
Correctly dumps my object in the console:
speaker:
FavoriteSpeaker {
name = Ashley Nelson-Hornstein;
}
But when I try to get the name property's value:
print("speaker name:")
print(RealmProvider.appRealm.objects(FavoriteSpeaker).first!.name)
I get an empty string 🤔
speaker name:
The four lines are together in my model's init method
Update 1: I found an answer that suggests that you merely don't see the values when printed in the Console: Realm object is missing all properties except primaryKey but I also tried displaying the name property via an alert view and that is also empty.
Update 2: Just to make sure that everything happens sequentially and on the same thread I did this:
let favorite1 = FavoriteSpeaker()
favorite1.name = "Debbie Downer"
try! RealmProvider.appRealm.write {
RealmProvider.appRealm.deleteAll()
RealmProvider.appRealm.add(favorite1)
}
print("speaker:")
print(RealmProvider.appRealm.objects(FavoriteSpeaker.self).first!)
print("speaker name:")
print(RealmProvider.appRealm.objects(FavoriteSpeaker.self).first!.name)
But the result is the same - printing name prints an empty string
The name property is probably not declared as dynamic, which leads to it reading the nil value stored on the object itself rather than reading the data from the Realm.
I'm trying to pass a string parameter in MVC3.
The URL that gets generated is:
http://localhost:50164/Property/Browse/Oregon
This appears to be the Property controller class in the function "Browse" with parameter "Oregon".
The right function gets called, but the string appears to be empty.
public ViewResult Browse(string location)
{
//counted_properties: 3 counted_properties_here: 0
ViewData["counted_properties"] = db.Properties.Count(); // debug 3 total
ViewData["counted_properties_here"] = db.Properties.Where(p => p.location == "Oregon").Count(); // debug 1 (the right answer!)
//return View(db.Properties.Where(p => p.location == tmp_location).ToList()); // 0 (bad!)
ViewData["the_location"] = location;
int size = tmp_location.Length; // unhandled null exception when tmp_location = location
ViewData["location_length"] = size;
// return View(db.Properties.Where(p => p.location == "Oregon").ToList()); // Right! but hardcoded
return View(db.Properties.Where(p => p.location == location).ToList());
}
When I return the lambda using the hard coded string (Oregon), I get the right answer.
When I return the lambda using the parameter (string location) I get zero results.
When I try to display ViewData["the_location"] which is a direct copy of the parameter it is blank. Maybe I'm not trying to display it correctly. Here is my view:
Location: #Html.Encode(ViewData["the_location"] )
When I call location.Length I get an unhandled null exception, but this works when I call Length on a hard coded parameter like "Oregon".
It looks like somehow the string is empty, but this doesn't make sense since I see "Oregon" in the URL when it gets to the property/browse function.
I'm beginning to wonder if I am allowed to pass strings as arguments in MVC as I haven't seen any examples with string parameters.
This is a continuation of an unsolved problem:
C#/ASP.NET lambda conversion
By the way, I'm not trying to promote Oregon here. I've never been there. Maybe it's a good place, though.
Check your routing. Traditionally URLs like the one you give will map the value ("Oregon") to the "id" parameter, rather than the location parameter that you're trying to use. Either rename the parameter or change the route to use "{controller}/{action}/{location}".