Hi, I want to inquire how to get results from a query if there are spaces in the field key. For e.g.
wixData.query(“Participants”) .eq(“lastName”, “Smith”) .find() .then( (results) => { let items = results.items; let item = items[0]; let firstName = item.firstName; console.log(firstName); } ) .catch( (err) => { let errorMsg = err; console.log(errorMsg); } );
In the above, what if the fieldkey is First Name (with a space in between) – how do you access that field? I have tried:
let value = item[1].[“first Name”]
let value = item[1].“first Name”
but neither of the above works.
Hi,
I would suggest you read the article About Database Collections . First follow the tutorial appearing at the beginning of the article, and then read about Regular Fields . When accessing a field in a query, you should use the Field Key . That will solve your problem.
Good luck,
Yisrael
Thanks Yisrael,
I did read the articles you mentioned. I am using Field Key - my field keys have space in them - and hence the question on how to access the field when the Field Key has space.
Note: I am populating my data collection using code from csv and perhaps this is why several of my field keys have space in between.
Make sure you are using Field Key and not Field Name . Field keys do NOT have spaces in them. See this image:
Thanks Yisrael. I am indeed using the Field Key.
I use the wix insert functionality to insert the rows into my data collection from a .csv file and looks like when the insertion is done in this manner, for column headers that have spaces in their names, the Wix platform creates Feild Keys with spaces for those column headers. Wix probably needs to address this in Wix Insert API or update documentation to say that column headers should not contain spaces. See attached:
Hi,
Thanks for the explanation and screen shot. I’m going to pass this over to the QA team and see what they say.
Hi tabraham,
You can access field by identifying field key in brackets in the item:
let item = items[0]
let valueFromFieldWithSpace = item[‘first Name’]
Let us know if it works for you,
Thanks!
Thanks Motiejus - that worked!
Motiejus / Yisrael:
I had two follow-up questions:
-
When I import a csv file using code (wix-insert api) into a Data Colletion – the fields are not added to the schema. Is there a way to ensure that when the fields are added they also become part of the schema? Doing this manually is not an option for me as I have over 300 data columns.
-
Is there a possibility to add additional columns to an existing row of a data collection using wix-insert api?
Thanks.
Hi Tabraham,
- When you use wix-data insert API, fields are not added indeed. Have you tried using Import functionality in Content Manager? It would add fields to schema for you.
- You can use wix-data update to insert additional data in additional columns for existing records in your database. Example:
import wixData from 'wix-data'
$w.onReady(async function () {
let item = await wixData.get('CollectionName', 'itemId')
item.newField = 'newData' //Add newData to newField in a row
let updated = await wixData.update('CollectionName', item)
});
Hope this helps
Thanks Motiejus, this definitely helps for the second aspect.
For the first aspect, I am updating the data collection from an external DB and would be triggering updates based on data and so have to do the import programmatically. Is there a way to accomplish adding the fields to the schema programmatically?
Thanks again.
Hi Tabraham,
Unfortunately there is no way to add these fields in any other way then manually clicking on it. But it is an interesting use case and I will pass it to our Product Managers.
Thanks
Or one more option is for you to prepare a csv file (export from the source) with fields you want to add in collection, and import this schema.
Thanks Motiejus, would be great to have this use case implemented – in the meanwhile we will look at the workaround you have suggested.