Update an object with notation with a parameter? - firebase

In my firebase i have a collection, inside there is a document, and inside there is an object :
object 1
key1:value
key2:value
key3:value
I would like to only update certain keys inside an object say
object1 - key1 and key2.
to do that, i need notation.
the problem is that I pass a parameter to the function that save :
function updateit(product,target)
{
db.collection("Stores").doc(target).update({
product
})
So here if I pass a product that contains only key 1, it will override the previous.
So, I tried to pass this object with notation :
product["product"+".title"] = "xxxxx"; // a new pair in product to pass
and it didn't work, it will save a new object (override) with fields like :
product
product.title=xxxxx
How should you do such a simple thing ?

ok obviously, this is the answer :
db.collection("Stores").doc(targetStore).update(
product // no {} around 'product', not as object!
)
see the comment that explains it all.

Related

Selecting Item in Array

I'm trying to run a query by using a user input. The query is run in a database which has multiple columns (practically a vlookup). The query is run and the output is an array of values.
How do I get the value of only one value (scalar output to put in alert)?
Thanks
I've tried to use methods familiar to java in order to call one field in an array without any success.
i.e. output_array[0]
// define input
// var custno = app.pageFragments.Add_SalesOrder.children.Form1.children.Form1Body.children.CustomerNo_Input;
var custno = 'ENC';
// define location of output
var outputWidget = app.pageFragments.Add_SalesOrder.children.Form1.children.Form1Body.children.CustomerName_Input;
// define datasource
var datasource = app.datasources.SalesOrder;
// query
datasource.query.filters.CustomerNo._startsWith = custno.value;
// load query
datasource.load();
alert(datasource[0]);
I expect to get the first entry in the array but instead I get 'Undefined'.
If you look at the datasource documentation, you'll find out that the datasource is NOT an array, but instead an object. Since you are looking for the first result of the query, then you should access the items property of the datasource. That is an array and you can then just access the zero index of the array to get what you need.
alert(datasource.items[0]);

Flutter: read large number of nodes in Firebase

I try to read data from Firebase and it doesn't work with a large number of nodes,
Here is what I tried :
Future<int> test() async {
final response = await FirebaseDatabase.instance.reference().child('...').once();
var urls=[];
response.value.forEach((v) => urls.add(v));
print(urls);
return urls.length;
}
Result :
It works well with some test data. Example:
but if I try to read 90 nodes, example :
Result :
(dynamic) => void' is not a subtype of type '(dynamic, dynamic) =>
void' of 'f
Any idea?
NEW
I found that if the key is for example :
-LU645_UgPbGZhBsneOq
(auto generated by a cloud function using .push().set() )
then it doesn't work.
More details : if the key is not numeric, then it doesn't work
Why?
If you use the node names given by push, then the value returned by once().value is a Map. Thus to iterate over it with foreach needs a function taking two arguments instead of one (which is exactly what the errormessage is telling you):
response.value.forEach((dynamic key, dynamic v) => urls.add(v))
this should give you in urls a list of maps with a "name" and an "url" key. The parameter "key" in the foreach function would be the nodename containing the name and url.
Why does a single-valued foreach function work with numerical node names? If you assign numerical node names firebase will return those nodes as an array. As your sample trees lack the "0"-Node you get "null" as the first element of the array.

Limit wp-rest-api posts request to an array of postID's

My complete project will need to return the nearest-posts to the given geolocation.
I succeeded in doing this by building a custom callback function seeking the nearest posts but also building a custom array containing the data-fields of those nearest posts. If I define this function as the callback function of my route all works great!
All ok but I would like the return to be a copy of the default post-return, of course limited to the nearest posts. And not a set of custom defined fields.
I can't seem to figure out how to do this.
Before going into details I would like to know which way to go. My plan, that doesn't work, was :
extend WP_REST_Posts_Controller
register (and initialize) a new route
define a custom callback function in this new route
the custom callback function builds an array of postID's that are the nearest (lat&long was passed in the http request)
So far so good but now comes my problem.
How do I pass the array of postIDs to the get_items method of WP_REST_Posts_Controller in order to get a "default" return of specific(nearest) posts
Clearly a simple approach like this doesn't work
public function custom_callback_function($geoloc_array) {
// call the function get_nearest_items($geoloc_array)
// that returns an array of postIDs'
// something like : [163,49]
$nearest_poi_array = get_nearest_items($geoloc_array);
// put the array of postIDs to fetch in "include" of a new array
// hand over new array to the get_items method of WP_REST_posts_controller
$posts_to_return['include'] = $nearest_poi_array;
return get_items($posts_to_return);
}
If somebody could point me to the right direction I would be grateful!!

what is #params in Iron:router

with meteor's IronRouter, I'm trying to use the this.params object elsewhere, but confused as to what it is. It seems to be a zero length array, that is actually an object with named methods after the path components.
# coffee
#route 'magnets',
path: '/magnets/lesson/:lessonCname'
data: ->
if #ready()
debugger;
console.log("route.params", #params)
with this code, in the debug console I will get:
this.params
[]
this.params.lessonCname
"despite-magnets-01"
typeof(this.params)
"object"
this.params.length
0
this.ready()
but in passing the params object to a server method, the methods (ie "lessonCname") disappear.
If my understanding is correct, then the near-term question is what is the best way to retrieve/convert these methods to {property:value} so they can be serialized and passed to server calls?
There are two easy ways of solving your problem, you can either set a global variable from within the data scope (but this is considered bad practice, at least IMO) or you can use the "data" function, which returns the data context for the current template:
data: ->
window._globalscopedata = #params.whatever #setting global variable
return someCollection.findOne #returns data context
_id: #params.whatever
when proccessing this route I will have the whatever param available in _globalscoredata and my document available in the template context.
Take a look at the source code for retrieving the parameters from a path. params is an array, but may have named properties. To iterate over everything, you can use the for in loop:
for(var x in myArray){
// Do something.
}
In this way, you can copy over everything to a new object (there may be a simpler way to create a copy).
The params property attached to a RouteController is an object with the following properties :
hash : the value of the URL hash.
query : an object consisting of key/value pairs representing the query string.
a list of URL fragments with their name and actual value.
Let's take an example, for this route definition :
// using iron:router#1.0.0-pre2 new route definition
Router.route("/posts/:slug");
And this URL typed in the browser address bar : /posts/first-post#comments?lang=en
We can use the console to find out precisely what params will actually contain :
> Router.current().params
Which will display this result :
Object {
hash: "comments",
slug: "first-post",
query: {
lang: "en"
}
}
Here slug is already a property of the params object whose value is "first-post", this is not a method.
If you want to extract from params these URL fragments as an object of key/value pairs, you can use underscore omit :
// getting rid of the hash and the query string
var parameters=_.omit(this.params,["hash","query"]);

filtering collection from backbone with attributes

I've got a backbone collection and I'm trying to filter by an id within the attributes
basically, a user has classes, and the class has a location_id, and I want to filter by the location id. My collection looks like this to give you an idea.
-user
-models
-0
-attributes
-location_id
-1
-attributes
-location_id
-2
-attributes
-location_id
I thought I could filter this by using
var get_locations = user_class_collection.filter(function(classes){
console.log(classes);
return classes.get(location_id)==location.id;
});
console.log(get_locations);
but that is returning an empty array, when I know the location_id is in the collection.
Any idea why this isn't working? I've also tried to grab classes.attributes.get, but it wasn't any better
In the first few responses, it was properly mentioned that I had to quote the get('location_id'). I've now done that, but unfortunately, I'm still getting an empty array. I thought that the filter would loop through the classes and I would get a console output for each class, but the console.log(classes) is only getting triggered once. Is that a hint? Or a red-herring?
you are trying to get a property from classes that is named as the value of the location_id parameter
you should instead make that a string (in fact you can choose how you make it a string, single or double quotes both work)
user_class_collection.filter(function(classes){
return classes.get('location_id') == location.id;
});
For filtering collection using backbone the best approach is to use a filtered function in your collection
var UserCollection = Backbone.Collection.extend ({
filtered : function ( id ) {
I suggest to use UnderScore filter which will return true for valid and false for invalid where true is what you are looking for. use this.models to get the current collection models use model.get( '' ) to get the element you want to check for
var results = _.filter( this.models, function ( model ) {
if ( model.get('location_id') == id )
return true ;
return false ;
});
Then use underscore map your results and transform it to JSON like
results = _.map( results, function( model ) { return model.toJSON() } );
Finally returning a new backbone collection with only results
return new Backbone.Collection( results ) ;
Optionally if you don't want to keep all the data in the collection but just the filtered one you should reset the collection and skip the above return like
this.reset( results ) ;
View call the filtered collection and the render the result
Try this:
user_class_collection.select(function(classes){
return classes.get("location_id")==location.id;
});

Resources