"Cannot read property '0' of undefined" when reading environment variable from mongoDB - html

I am trying to read a variable which is defined in my javascript. It is a get request from a mongoDB. The whole database is then stored under a variable and is read by the HTML and displayed.
The get request from the MongoDB has an output like this:
(Lets say this is stored under the variable database):
[
{
0:
{
TITLE1: valueone
}
},
{
1:
{
TITLE2: valuetwo
}
}
]
My HTML looks like this:
<p> {{ database?.TITLE1 }} </p>
I get the error Cannot read property '0' of undefined. I understand this is because I need to define [0] to be able to read TITLE1.
Based on this I have tried the following:
<p> {{ database?[0].TITLE1 }} </p>
This has this error: Template parse errors:
Parser Error: Conditional expression database?[0].TITLE1 requires all 3 expressions at the end of the expression [{{ database?[0].TITLE1 }}]
<p> {{ database?.[0].TITLE1 }} </p>
This has this error: Template parse errors:
Parser Error: Unexpected token [, expected identifier or keyword at column 7 in [{{ database?.[0].TITLE1 }}]
<p> {{ database?.0.TITLE1 }} </p>
This has the same error as the one above.
What is the correct way to be able to read the values that I am after. In the HTML the output should be valueone.

Because your database is array, use need use [0] to get first item and your key is number so you need use ['0'] to get property value, you also can use ? to check object null before using TITLE1
Finally you can use {{ database[0]['0']?.TITLE1 }}
Demo https://stackblitz.com/edit/angular-o79rra

Well I mean, at some point you have to think a little about JS basics instead of trying everything until it works ...
From the code you have provided, the syntax would be
{{ data[0]['0'].TITLE1 }}

Try
<p> {{ database[0]['0']?.TITLE1 }} </p>
Do it dynamically by:
<p *ngFor="let item of database; let i = index">
{{item[i]['TITLE' + (i + 1)]}}
</p>

Related

How to pass variable while calling macros in post hook?

Currently I am calling macros in such a way:
"post_hook":["{{macros_append('string1','string2')}}"]})}}
I want to call it as
"post_hook":["{{macros_append(var1,var2)}}"]})}}
I have already tried setting variable before config like
{% set var1='value' %}
"post_hook":["{{macros_append(var1,var2)}}"]})}}
But this does not work. It does not take any value while calling macros.
This doesn't work because dbt is parsing the jinja of your post-hook in a different context from the model file. In short, the post-hook needs to stand on its own.
This is true of all configs. The hints are:
post_hook takes a list of strings. Those strings later get templated by the jinja templater. (this is why you quote them and nest the curlies! you should never really nest curlies otherwise)
Configs can also get passed in through .yml files, etc., which is partially why the templating is deferred
Your question omits the actual call to the config macro, which makes this a little more clear:
{{
config({
"post_hook": ["{{macros_append('string1','string2')}}"]
})
}}
So what are we to do? You could use jinja to build the string that gets passed into the config block. This is hacky and ugly, but it works:
(Note that ~ is the jinja string concatenation operator.)
{% set var1 = "string1" %}
{% set var2 = "string2" %}
{{
config({
"post_hook": ["{{ macros_append(" ~ var1 ~ "," ~ var2 ~ ") }}"]
})
}}
A slightly cleaner version of this would be to define the whole macro call in a variable, so you don't have to do the concatenation:
{% set my_hook = "{{ macros_append('string1', 'string2') }}" %}
{{
config({
"post_hook": [my_hook]
})
}}
A Better Way
Another option is to use the var() macro, which allows you to access a global variable in the jinja context. You define these global variables in your dbt_project.yml file:
...
vars:
var1: string1
var2: string2
and then you can access them with {{ var('var1') }} from any process that is templating jinja. In the case of your config block, that would look like:
{{
config({
"post_hook": ["{{ macros_append(var('var1'), var('var2')) }}"]
})
}}
Note that the post-hook here is just a string that contains the string "var('var1')", but that's fine, since the templater will fill that in later, when the string is templated.

How to use mustache tag in json file and get value from Vue?

I am trying vue-i18n and have keep the translation in files according to language.
en.json
{
"Msg1": "You have ",
"Msg2": " results."
}
showResult.vue
{{ $t('Msg1')}} {{totalResults}} {{ $t('Msg2')}}
Which is able to show the sample output, but let's said if I want to combine all together in single line at en.json, eg:
{
"Msg1": "You have {{totalResults}} results."
}
And for showResult.vue I have changed to, which I am not sure is it a correct way:
{{Msg1}}
And below is the sample output I hope to get:
You have 10 results.
The 10 is value from {{totalResults}}, which will change accordingly.
How can I do so?
Thank you.
{{ $t('Msg1', { totalResults: 10 }) }
https://kazupon.github.io/vue-i18n/guide/formatting.html#named-formatting

How to pass a concatenated string as a parameter to function: Angular 2

I have the following code:
...
<tr ngFor= "let i = index">
<myCustomElement myName="{{'nameEdit'+i}}">
<button
<--This is where I get the "Got interpolation ({{}}) where expression was expected" error-->
(click)="myFunction(requestForm.controls.'nameEdit'{{i}}.value)">
</button>
</myCustomElement>
</tr>
...
My goal is pass to myFunction the value of nameEdit for each element (so this will be nameEdit1, nameEdit2, nameEdit3 and so on. My existing code results to an Got interpolation ({{}}) where expression was expected error.
What's the proper way to pass my value to myFunction?
(click)="myFunction(requestForm.controls['nameEdit' + i].value") should do the trick
Since double quotes for event directives (...) are interpolated, the {{ ... }} is unnecessary. You will need to also use the javascript object identifier [...] with the dynamic text.
Lastly, this will obviously return error if the controls doesn't have a key with the name you're trying to parse. It would be best practice to have myFunction(...) manage this case.
Working stackblitz example that outputs the values: https://stackblitz.com/edit/angular-whq8ll-od64hx?file=app/slider-overview-example.html

Get the value of a JSON array with _attribute

I have a strange looking JSON file (I think?) generated from elasticsearch.
I was wondering if anyone know how I could retrieve the data from a JSON object looking like this:
u'hits : {
u'hits : [{
u'_score' : 2.1224,
u'_source' : {u'content': u'SomethingSomething' }
}],
u'total: 8 }
u'took: 2 }
I can retrieve the total by writing {{ results.hits.hits.total }}, however, the underscore symbol (_) in front of the attribute name "_score" makes it impossible to retrieve the value of that attribute.
Any suggestions?
Try:
{{ results.hits.hits[0]._score }}
{{ results.hits.hits[0]._source }}

Combining two variables for use in Twig

I'm using Google app engine and I'm trying to set the value of a textarea based on the concatenation of two string variables. Let's say I have 4 items, and each item has multiple fields. So in my Python I'm passing the dictionary { 'value0': newValue }. I'm using a for loop (iterator value num) and I want to use in my HTML something equivalent to {{ value }}{{ num }} where the variable referenced is value0.
I've tried {{ value~num }} but nothing works. If I could use an array that would be even better - such as {{ value[num] }}