Hi guys,
I have a collection called "Blog", this collection have some numbers of fields: title, subtitle, content, status etc...
When I run a the below query it returns an object with the record expected, but this object does not contain all collection fields (for example it does not return the subtitle field and its value).
$w.onReady(function () {
foo2()
});
async function foo2() {
let recordRetrieved = await wixData.query('Blog').eq("_id", '71b7d9b9-a6bd-49b0-86bd-836f00483c4f').find()
console.log(recordRetrieved.items[0])
}
The collection has not restriction.
The problem appears in both the front-end and the back-end.
Any help is appreciated.
I see what you're saying, but that's just the behavior of the API functions. If the field is undefined, then there's nothing to return. Furthermore, undefined is not a valid JSON value.
Taking care of it in your insert would be better as it's more reliable and ensures against having invalid (undefined) deta. Something like this:
let toInsert = { status: 'draft', email: null, name: null, lastname: null, ... and so on ... }
Note: you can use null, or "", or some other default value.
BTW - you can use get() instead of query, it's just a little easier.
let collectionItems = await wixData.get(currentCollection, currentRecordId);
Thanks Yisrael,
this is the link https://editor.wix.com/html/editor/web/renderer/edit/19b68635-b258-45ad-b6ed-b94abac52a16?metaSiteId=fb39f05b-80eb-4ab3-a30e-8488bddecf1f&editorSessionId=a69174c4-b64a-478d-9e9b-b4ce83551af7&referralInfo=dashboard
I prepared a page and collection called "Yisrael" for your test because my page is a bit complicated with a lot of functions and it's easy to get lost.
I hope you can understand the issue explained in the code and in the form, English is not my primary language. :-)
Thanks.
Yisrael,
"You can check if a field is undefined, and if so then provide a null or default value": I can check if the field is undefined until "wixData.query" or "wixData.get" also returns the value of the empty field.
Alternatively I can try to do it manually by calling each field within my function, but try to think about doing it with a collection of 100 fields or replicate this behavior for 50 collections. In my opinion this is not the right choice.
Also, I am not able to define a default value at collection level with the collection builder, I have to manage it in the code.
I was thinking about implementing a web application using also Wix databases capability: this is the first limit I found and it makes me start thinking about another solution.
Thanks.
The empty fields are probably being returned as undefined since they don't have values. You can check if a field is undefined, and if so then provide a null or default value, something like this:
if(item.field1 === undefined) { item.field1 = 'some value'; }
Note: it's simple example so I didn't bother testing. But something like this should work. There are other ways to do this which you can find with an Internet search.
Another way would be to make sure that all collection fields have some sort of default value.
I checked and I can confirm that the problem is related to empty values.
My use case is the following:
I need using this query to update the collection. Following the documentation at this link https://www.wix.com/corvid/reference/wix-data/update I have to get the record and then update it, but if the query does not return those fields, how can I update them accordingly? Why don't return and object with all keys/fields and null values if they are empty?
Thanks
Thanks for your replay... Those fields do not have a value.
If this is the issue, is there a way to return all the fields in the collection, including the empty ones?
Thanks
Do those fields have values? What does the collection look like?