handlebars find and retrieve object based on other object iteration - object

i have an array holding objects from the server like this:
[Object, Object, Object, Object, Object]
so i'm doing an #each on that array, and i'm starting to display each value in the current object using the this keyword for example :
{{#if this.recommendations_received}}
{{this.recommendations_received.length}} recommendations
{{else}}
No recommendations
{{/if}}
each object has an id like so :
object : { id: "someid" ,
recommendations_received: [
{
recommendation_id: "some_ID",
giver: {
giver_id: "the_giver_id",
name: "the_giver_name"
}
}
]}
so i'm doing another iteration to show each recommendation like so:
{{#each this.recommendations_received}}
{{this.giver.name}}
{{/each}}
but now i need also to iterate over the giver categories array. which simply look like this
categories : [object, object, object]
inside each object in categories i have name and title.
i want to get the categories of the giver recommendation inside the #each helper that i did for recommendation_received based on the id of the giver which is unique.

You haven't shown how is giver and categories related to each, but you need to combine your objects before passing to handlebars. The easier way is to simply combine both objects by nesting the categories object inside the giver object. The other way would be to create a wrapper object to hold your main array and the categories array before passing to handlebars.

Related

Update specific field in an array of object in a MongoDb Document

I have a document with that format :
{
"field1":"string",
"field2":123,
"field3":
[
{
"subField1":"string",
"subField2": object,
// other fields after
},
{
"subField1":"string",
"subField2": object,
// other fields after
},
// other fields after
]
// other fields after
}
I want to update at once the field3.$.subField2 field only, with a different value in each of the elements of the array composing field3.
Does anyone knows how to properly do it ?
One reason I want to do this is I have several asynchronous operations that are meant to update differents fields of the array field, and I have concurrency issues...
I tried with findOneAndUpdate(query, $set{"field3.$.subField2": arrayOfValues}) but it does not works, it seems I can only pass one single value that would be set to each element of the array (all the same).
arrayOfValues would be of course an array with only the values I want with the matching indexes and same size of the document's array.

YUP NODE JS flat dictionary object validation

I have a form data like this
labels: { 'EN-US': 'foo bar', 'EN-BR': 'foo baz' }
How to validate it through YUP? where there are no names for the Object while I searched a lot to create object names through HTML without Js m But I did not find a way, So I have to send them flat, but there is really no reference to validate such dictionary object?
I want only the schema structure for both keys and values and I will do the rest .. consider I need them Just strings?

Firebase Cloudfirestore update elements in a collections

i'm using firebase functions with nodejs and cloudfirestore to build an API, now i have a problem, i have a collection an inside the collection an array of objects.
My goal is to update only a specific element without read the whole object and then push it again.
This is how my "order's collection" looks like:
"someRestaurantId":[{id:1,desc:"test",val:3000},{id:2,desc:"test",val:4000},{id:4,desc:"test",val:5000}]
My goal es only update id 2.
This is what i been trying:
Get the full list based on
const document = db.collection('orders').doc(req.params.restId);
Iterate and find the correct element, modify it and update it again.
await document.update(fullobject)
The problem is that i need something more easy to handle since there could be thousand of element inside that array.
Looking at described issue, I think you should use subcollection (sometimes called nested collection) for that. However from the description it is hard to tell what do you have now.
Firestore collection structure is always like this:
collection -> document -> fields and nestedCollections
than nestedCollection has the same structure, so you can nest whole structure how many times you want, but always collection contain only documents and each document contain only fields and collection.
In presented collection description array is directly in the collection or directly in document which both are impossible.
Anyway my idea is to structure it following way:
Collection orders containing docs of various restIDs
every docs (ex. "someRestaurantId") contain subcollection with name ex. "RestOrders"
in the subcollection will add documents with ids like (1,2,3 etc.)
every doc in subcollection will have fields desc, val
Collection Orders:
Document RestID_1:
Subcolection RestOrders:
Document 1: {val: 3000, desc: test}
Document 2: {val: 3000, desc: test}
...
Document RestID_2:
Subcolection RestOrders:
Document 1: {val: 3000, desc: test}
Document 2: {val: 3000, desc: test}
...
And now to update value of "RestID_1", order "id 2" you can use:
db.collection('Orders')
.doc('RestID_1')
.collection('RestOrders')
.doc('2')
.update( {val: 4000} )
In above solution you avoid reading and updating big array. You can update only one filed of one document in nested sub-collection.

How to render only one document in jade view?

I need to display only one document from my CouchDB, but if I don't create for-each loop, I can't define variable employer
.row
.col-xs-12.col-sm-8
.row.list-group
each employer in the employers
.col-xs-12.list-group-item
h4
p #{employer.value.name}, #{employer.value.location} #{employer.value.type}, #{employeer.value.position}
small
.col-xs-12.col-sm-4
p.lead= sidebar
When I try to display only one document without the loop, I am receiving an error: Cannot read property 'value' of undefined
If employeers is an array, you should be able to do something like:
employeers[0].value.name
If you want to keep using "employer" you can also replace your each loop with:
- var employer = employeers[0];

How to get YUI object on page

I'm using the YUI2 framework.
I want to be able to get a reference to an object that was created on the page. I don't want to change the existing JS code to store a global handle to it when it is created, if possible.
The page has something like this on it:
<script type="text/javascript">//<![CDATA[
new FirstRate.Reporter("report1").setOptions(
{
inData: "testfunc"
}));
//]]></script>
I don't see any methods in YAHOO.util.Dom specific to finding objects.
How can I get an object reference to FirstRate.Reporter? Is there anything to ennumerate the objects on the page?
Thanks
YUI contains lots of methods for finding objects in the webpage DOM. YAHOO.util.DOM contains methods:
HTMLElement | Array get ( el )
Array getElementsBy ( method , tag , root , apply , o , overrides )
HTMLElement getElementBy ( method , tag , root )
Array getElementsByClassName ( className , tag , root , apply , o , overrides )
and many more. These retrieve objects from the DOM. To find an object in the page with YUI2, you would typically use some combination of the tag type, the class name or the id to query the page using YUI methods to find the object you want. It's easiest to find a specific object in the page if you give it a unique id value and then you can just using YAHOO.util.DOM.get("myObject") to retrieve it (where "myObject" is the id of the HTML element).
If you want to get regular javascript objects, then you have to store references to them yourself in your own javascript variables. YUI doesn't do that for you except when using some UI widgets (which also have DOM elements).
If you want to keep track of the result of this:
new FirstRate.Reporter("report1").setOptions(
{
inData: "testfunc"
})
Then, you have to assign it to a variable or to a property of an object whose scope allows it to last long enough for you to use it.
var theReporter = new FirstRate.Reporter("report1").setOptions(
{
inData: "testfunc"
})
or
myObject.reporter = new FirstRate.Reporter("report1").setOptions(
{
inData: "testfunc"
})
where myObject is some other object you've created and are storing.
JavaScript objects aren't part of DOM. DOM is the Document Object Model, it's about objects in HTML document like divs, forms, inputs etc, the kind of objects Browser displays.
There is no "global enumeration" of objects in JavaScript. There are global variables.
I don't want to change the existing JS code to store a global handle to it
So you don't want to use global variable. But why?
This is the only way to do it, and it's also very simple:
var myGlobalVar = new FirstRate.Reporter("report1").setOptions({inData: "testfunc"}));
Here you'll store reference ("handle" as you say) to your object in global var myGlobalVar, which you can later access in another part of your JavaScript code.

Resources