Firestore, problem retrieving field with custom key - javascript

I'm using firebase firestore to save and retrieve data for my project. For reference, my data structure looks like this:
collection
document
LO3N83COzWfu:[Array]
randomisedfield2:[Array]
I'm trying to retrieve the randomised fields from the document. When I log document data, everything seems to be fine. When I try to log the field as doc.data().LO3N83COzWfu , it also works. However, I couldn't find an answer as to how I can retrieve the field, when it's requsted using a variable key such as the following:
let key = "LO3N83COzWfu";
console.log(doc.data().$[key]);
Error getting document: TypeError: Cannot read property 'LO3N83COzWfu' of undefined
How can I retrieve the field in such a case? What is the query I need to use?

Related

How to retrieve data in front end from res.send and create page for each element in DB dynamically

I've got a variable, say data containing data in the form of an Array with each item having a unique ID.
app.get('/products/:id', function (req, res) {
res.send(data.map(data => "" + data.id + "")) //basically gets the data of the element in the Array whos Id has been given to the server.
})
I have sent the data from the server to the front-end on a GET request. But how do I create a seperate webpage for each element dynamically in the data array? where do i have to write the html and the css? I want a way with which I can create a page for each element like domain.com/products/id which displays information about the data entry which matches the Id . Do need to use pug? hbs?ejs? I' so confused.
So I found I had to use Javascript Templates to send data to a view. I used ejs, which went pretty good!
Here's how it went:
1. fetch my data form my DB, which in this case is MongoDB using db.findOne().
2. We get an array, let's say data. send the variable to my view using the same res.render syntax, just in a cooler way.
app.get('/blogs/:id',(req,res)=>{
const data = //find function
res.render('page.ejs', {body:data});
})
:id creates a page for every element in the DB.
and now the view that is, the public/page.ejs file has a global body variable, which
we can now use to show our blogs.
3. the front end markup in pages.ejs:
<div class="blogs">
<%=body.forEach (item)=>{%>
<p><%=item.blog%></p><br>
<%=}%>
</div>
We call a forEach function on the array, and create a paragraph element for each item in the array, that is for each blog.
Please not that <%, <%= and %> are EJS' tags. Read about them more in the official docs.
Thanks Mohammad for letting me know about this. (From comments)

How to append (non duplicate) data to firebase db using javascript?

I am trying to use Firebase realtime db .I am trying to insert following form data in to firebase db but it does not use the name2 variable from form, instead it just insert name2 into db? Furthermore, how I append data to this db instead of updating same data every time i submit the form ? I considered using push but i don't want to create unique id/key each time in insert data!
Update: Now I am able to insert form name by using :
var playersRef = firebase.database().ref("players/"+ name2);
javascript:
function writeData5()
{
name2=document.getElementById("nameField2").value;
number2=document.getElementById("numberField2").value;
age2=document.getElementById("ageField2").value;
alert("name:"+name2+" number:"+number2+" age:"+age2);
var playersRef = firebase.database().ref("players/");
//playersRef.set ({john:{number:+number2,age: +age2}});
playersRef.set ({name2:{number:+number2,age: +age2}});
}
db structure I want to achive:

how can I access to a specific json value in my returned data

I send a http GET request which returns JSON data in the following form. I am finding it impossible to access this data despite having no problems with example json that I create. I want to ideally take the array under wifi, and use this data to create a html table, but I think I can work out how to create the table if I could just access the actual elements of the data.
I have tried multiple methods to try and reach the first timestamp. I have tried:
var element = result.undefined.clients.wifi[0].timestamp;
but this returns an error that 'clients' can't be found.
I also tried:
var element = result.clients.wifi[0].timestamp; //and
var element = result.wifi[0].timestamp;
The JSON data returned to a variable is shown below:
result = undefined
{"sourceId":"idid","sourceType":"CLOUD_source","searchMeta":{"maxResults":4,"metricType":["clients"],"family":["wifi"],"Interval":"M1"},
"clients":{"wifi":
[{"timestamp":1424716920,"avg":3,"min":1,"max":4,"Count":8,"sCount":3,"sources":["x1","x2","x3","x4","x5","x6","x7","x8"]},{"timestamp":1424716980,"avg":2,"min":1,"max":3,"Count":4,"sCount":2,"sources":["x3","x4","x8","x4"]},{"timestamp":1424717160,"avg":2,"min":1,"max":3,"Count":9,"sCount":4,"sources":["x3","x4"]}]}}
The JSON data is invalid. If it is returned from a server, you need to go there and correct the data source, (if you have access to that).
Otherwise, perhaps notify the backend guy(s) about it.
If it is from a REST API, and you are "sure" that the server code should be error free, then check that you have supplied all the required parameters in the API request you are making.
I think your JSON is messed up. I ran it through JSONLint, and having the undefined at the beginning causes things to break.

How to retrieve the list of positions including all the fields using the LinkedIn javascript sdk?

I want to get the id, title, summary, start-date, end-date, is-current and company name for each position that a LinkedIn member has entered on its profile.
I tested a query on the REST Console and I got the desired result. The query is "https://api.linkedin.com/v1/people/~/positions:(id,title,summary,start-date,end-date,is-current,company)?format=json".
The problem is that when I try to do the same thing using the LinkedIn javascript SDK I'm not getting the whole list of fields for each position. I'm getting the complete list of positions, but not all the fields for each position.
This is the call that I'm doing with the javascript SDK:
//in this function I'm specifying the list of fields that I want to retrieve for each position
//but I'm getting only some fields (id, start-date and end-date)
function getProfileData() {
IN.API.Raw("/people/~/positions:(id,title,summary,start-date,end-date,is-current,company)?format=json").result(onSuccess).error(onError);
}
Somebody has any idea of what I need to do to get the same result than using the REST Console?
Requesting URL is wrong.
Use following for positions
https://api.linkedin.com/v1/people/~:(id,positions)?format=json
Use following for locations
https://api.linkedin.com/v1/people/~:(id,location)?format=json
If you want to retrieve your profile data use this,
IN.API.Profile("me")
.fields([
"firstName","lastName","headline","positions:(company,title,summary,startDate,endDate,isCurrent)","industry",
"location:(name,country:(code))","pictureUrl","publicProfileUrl","emailAddress",
"educations","dateOfBirth"])
.result(onSuccess)
.error(onError);
(or) If you want to retrieve other profile data then replace "me" with "<public profile url>"
IN.API.Profile("url=https://nl.linkedin.com/in/williamhgates")
.fields([
"firstName","lastName","headline","positions:(company,title,summary,startDate,endDate,isCurrent)","industry",
"location:(name,country:(code))","pictureUrl","publicProfileUrl","emailAddress",
"educations","dateOfBirth"])
.result(onSuccess)
.error(onError);

How to get values from the store in extjs4

I have a store that fetches data from the zend server. I want to get the store records to do some customizations on my form. For getting data from store i am using the below code.
var index = Ext.StoreMgr.lookup('product.AttributeComboBox').find('abbr',4);
var reco = Ext.StoreMgr.lookup('product.AttributeComboBox').getAt(index);
Above snippet returns no records. Please let me know where i am wrong.
In your debugger check the store exists
Ext.StoreMgr.lookup('product.AttributeComboBox')
Check how many records are in the store
Ext.StoreMgr.lookup('product.AttributeComboBox').data.items
Check the records have parsed properly
What came from the server for the record
Ext.StoreMgr.lookup('product.AttributeComboBox').data.items[0].raw
How it get converted into the record
Ext.StoreMgr.lookup('product.AttributeComboBox').data.items[0].data
Can you show us more code ?
So far, seems ok but you will have to check if the store have been created and if it has all the records, just like RichH said.
To check if the Store exists I would do
var productStore = Ext.getStore('product.AttributeComboBox');
console.log(productStore );
To check if the store is loaded
console.log(productStore.getCount());
To find the record
console.log(productStore.findRecord('abbr','4'));

Categories