In my Angular app I'm returning results via an API call, and allowing users to filter those results through a series of checkbox selections. Right now I'm running into an issue where, while results are returned as expected when one value is sent for a certain filter, when multiple values are selected (like filtering by more than one zipcode, for instance) I get zero results printed to the view. No errors, just no results.
After scratching my head for a while, using Chrome devtools network tab, I finally determined that the problem is that rather than wrapping each item in quotes in the payload - like this: "90001", "90002", what's being sent is this: "90001, 90002". In other words, quotes are wrapped around as if it were one value, not two.
This is the code, where I'm using a "join" to put together the values that are selected:
this.sendZipcode.emit(this.zipcodeFilters.text = arr.length === 0 ? undefined : arr.join(','));
I'm not sure how I can adjust the way this "join" is constructed, or find some other solution instead of "join", in order to have each item wrapped in quotes, rather than wrapped like one long string.
FYI, this is what I see in the network tab of Chrome devtools after selecting two zipcodes. As I explained, it's wrapped like one string, rather than as two values:
addresses.zipCode: {$in: ["90001, 90002"]}
Array.prototype.join will return a string. So you are converting your array to a single string which is being sent as a string. You want to send an array. Simply remove the join call and return arr directly.
String: "value, value"
Array: "value", "value"
Related
I have a question that I feel should be pretty simple to answer, but I cannot find fix anywhere.
I'm querying the Uberchord website for guitar chords (documentation here)
Here is the URL I'm querying:
https://api.uberchord.com/v1/chords?nameLike=${variable}
Where variable is based on what the user types in.
The nameLike lets fuzzy match between the input and the API (For example, the url for an Fmaj7 chord is F_maj7). This works great, but it returns multiple responses. The first response is always the one I want, so I'm trying to just limit the responses to the first one that comes in. So far, I've tried adding a limit or a top filter to the URL to no avail. This is where I'm sure I'm doing something incorrect.
For limit, I've tried:
https://api.uberchord.com/v1/chords?nameLike=${fmaj7}?limit=1
and
https://api.uberchord.com/v1/chords?nameLike=${fmaj7}&limit=1
and for top I've done the same two but just replacing limit with top. The first one returns zero responses, while the second returns all responses.
Any ideas?
It isn't literally "returning multiple responses". It's just returning an array. It ALWAYS returns an array, even if there is only one element. If you only want the first element of that array, then fetch the first element of the array, just like you would with any other array. After you do the JSON decode:
chord = response[0];
Consider the code:
const id=aux[0].healthyTissue[0];
const aux4 = await Hidden.findById(id).populate(
"children"
);
As you may have noticed, aux is an array and so is aux[i].healthyTissue[0].
Problem: I want to gather all the information in a single array called aux4, as I am doing for index equal 0, I want an automatic way to search inside all the arrays.
I have tried something like ForEach and map, with no success, I cannot make it wait for the result, it keeps coming empty on the following res.json I thought about pipe, however, I could make it work on this case.
Obs. the code showed works, but I want to go through all the elements, gather them in a single array, and return in a res.json
I have a domino view with an amount column but some values are empty...and need to be. The problem is that the #Sum works fine until I have an empty value then it stops summing.
eg: if the values are 5,5,"" and 5 I get a sum of 10 and not 15.
I've traced the problem to the #DbLookup which is that it stops building the return array when it encounters a blank value. There is no built in method of dealing with null values.
https://www.ibm.com/support/knowledgecenter/en/SSVRGU_9.0.1/reference/r_wpdr_atfunctions_dblookup_r.html
To make things harder, #dbLookup returns a string if only one is found or an array if more than one are found. If the values are 5,5,"" and 5 it returns an array of 2 values.
var alloc = #Sum(#DbLookup(#DbName(), "SubForms",MainFrmID , "ca_ca_ca_ca_amount"));
if (isNaN(alloc)){
return "$0.00";
}else{
return "$" + alloc.toFixed(2);
}
Can anyone help me refactor the #sum or #DbLookup to allow for empty values? Unfortunately I cannot define any new functions for this solution. The environment is locked down tightly. With a list of values of 5,5,"" and 5 I need a sum of 15.
I would try #Sum(#TextToNumber(#Trim(#Text(#DbLookup(...)))))
I would try
#Sum( #Transform( #Dblookup ( ....
If #DbLookup does not do what you need, you could always iterate over documents or view entries to build the sum.
The flow would be roughly like this:
1. Get a handle to the current database.
2. Get a handle to the "SubForms" view.
3a. Get a view entry collection using using getAllEntriesByKey() with MainFrmID as key, if a view column exists that displays the values you need.
--OR--
3b. Get a document collection using getAllDocumentsByKey() with MainFrmID as key, if no view column exists that displays the values you need.
4. Iterate over the collection to sum up values, using getColumnValues().get(columnNumber) to access the value from each view entry, or getItemValueDouble(fieldName) to access the value from each document.
That way you can easily detect null values and discard them.
I am making a cross domain AJAX request, and because of this, I can't pass the original array from the PHP page to my HTML page. Instead, the request gets the results as a string. I have it set up so that the syntax looks like this:
([SCHOOL] Name [/SCHOOL][STATUS] Status [/STATUS])
([SCHOOL] Other name [/SCHOOL][STATUS] Other status [/STATUS])
On my HTML page, I want to be able to form an array from these different values, in the form of:
Array (
[0] =>
[0] Name
[1] Other
[1] =>
[0] Name
[1] Other status
)
This way, I can use a for loop to get specific values.
The only problem with is that split only does just that, splits things. Is there a way in JS to find the text within separators, and form an array from it? In the example again, it'd find all text within the parenthesis, and form the first array, then within that array, use the text between [SCHOOL][/SCHOOL] for the first object, and use the text between [STATUS][/STATUS] for the second object.
Ideally, you would use something more suited to storing arrays on the server side. I would recommend JSON. This would be trivial to encode using php, and decode using javascript.
If you do not have that option server side, then you can use regex to parse your text. However, you must be sure that the contents does not have your delimiters within in it.
It is not clear how you get your target data structure from your source, but I would expect something like this might work for you:
str = "([SCHOOL] Name [/SCHOOL][STATUS] Status [/STATUS])\n\
([SCHOOL] Other name [/SCHOOL][STATUS] Other status [/STATUS])"
arr =[]
m = str.match(/\(.+?\)/g)
for(i in m){
matches = m[i].match(/\(\[SCHOOL\](.+?)\[\/SCHOOL\]\[STATUS\](.+?)\[\/STATUS\]\)/)
arr.push([matches[1],matches[2]])
}
console.dir(arr)
Basically, I'm working on a page that includes four different JSON "thingies" (objetcs,arrays). Forgive my lack of proper terminology.
When I get the JSON, it comes in as an object with a bunch of sub-objects, and each "sub-object" looks like this:
"token":"government",
"title":"Government",
"isSelected":false,
"type":"CATEGORY",
"subtype":"INDUSTRY",
"count":12
So the first task is to loop through each JSON and populate a box full of checkboxes, using the title as the label and the isSelected to indicate the checked status. So far, so good.
BTW, somewhere aslong the way, I picked up a JS script that checks whether an object is JSON or an array, and according to that "quick & dirty" test, my object is an array. Or an array object (you know, the one is created with [ ] and the other with { })?
Anyway, when the end user checks and un-checks checkboxes, I need to keep track of it all and immediately send back changes to the server (when the user clicks a DONE button). The crazy thing is that by looping through the objects, I was able to change the isSelected value to true . . . just not back to false.
for(var i = 0; i < $array.length; i++){
$array[z].isSelected = true;
}
Perhaps I was up too late when I worked on all of this, but using the same approach, I could not change $array[z].isSelected to false when the checkbox got de-selected.
In the end, I converted the JSON "thingy" to a string, search and replaced the corresponding values, and then converted the string back into an object. This is all working now, but I feel as though I've just used up a roll of duct tape on something that could have been put together by snapping the pieces together nicely.
Question: Did I miss the boat totally and is there a simple way to change values of JSON objects?
If so, could you point me in the right direction?
That JSON thingy is just a string representation of a javascript object.
One way of creating an object is
var myObject = {
"myName": "AName",
"myType": "AType"
};
This object can be referenced as myObject, with the properties myObject.myName and myObject.myType containing values AName and AType.
You should be able to just reference the object by name as objName.token objName.title etc.
If you have trouble try parsing the json with javascript then reference the result as above. This should make it easier for you to access, manipulate or delete data in the objects properties as well.
The nesting of these as below can be referenced as myObject.moreProperties.prop1 etc
var myObject = {
"myName": "AName",
"myType": "AType",
"moreProperties": {
"prop1": "vaue1",
"prop2": "vaue2",
}
};