Array

Hi everyone !!!
I am simply having an array here

[{
                    rating: 'rating 1',
                    value: '2'
                },
                {
                    rating: 'rating 2',
                    value: '0'
                },
                {
                    rating: 'rating 3',
                    value: '5'
                }
            ];

So, when I put these in the database in an array field, it gives an error (The value does not match the field type Array) and automatically converts it into another form →

[
  "[{\n                    rating: 'rating 1'",
  "value: '2'\n                }",
  "{\n                    rating: 'rating 2'",
  "value: '0'\n                }",
  "{\n                    rating: 'rating 3'",
  "value: '5'\n                }\n            ];"
]

So on my page, I having this code for searching in an array →

let arr = [{
                    rating: 'rating 1',
                    value: '2'
                },
                {
                    rating: 'rating 2',
                    value: '0'
                },
                {
                    rating: 'rating 3',
                    value: '5'
                }
            ];
            
 let obj = arr.find(o => o.rating === 'rating 1');
 let rating1 = obj;
            console.log(obj);

 let obj2 = arr.find(o => o.rating === 'rating 2');
 let rating2 = obj2.value;
            console.log(rating2);

 let obj3 = arr.find(o => o.rating === 'rating 3');
 let rating3 = obj3.value;
            console.log(rating3)

This code works when I define the array as ‘arr’

But don’t work when I query my database and then defining the array →

 wixData.query("Topic")
        .isNotEmpty("rating")
        .find()
        .then((results) => {
 let items = results.items[0];
 let arr = items.rating
            console.log("arr " + arr)
 let obj = arr.find(o => o.rating === 'rating 1');
 let rating1 = obj;
            console.log(obj);

 let obj2 = arr.find(o => o.rating === 'rating 2');
 let rating2 = obj2.value;
            console.log(rating2);

 let obj3 = arr.find(o => o.rating === 'rating 3');
 let rating3 = obj3.value;
            console.log(rating3)

 let obj4 = arr.find(o => o.rating === 'rating 4');
 let rating4 = obj4.value;
            console.log(rating4);

 let obj5 = arr.find(o => o.rating === 'rating 5');
 let rating5 = obj5.value;
            console.log(rating5);
        });

Gives me this error →

Any ideas why this error come ?
Thanks !!!

I tried adding your array to a test database and it accepted it just fine:

What am I doing wrong? Or right?

Ajit, maybe you just don’t have a record with ‘rating 2’ in your collection.

Hi Yisrael !!!
Thanks ! I did it but giving this error →

(-After typing, click on another field)

Thanks J.D
But I didn’t understand what you said :slightly_smiling_face:
‘rating’ is the (array) field where the below array is stored

[{
                    rating: 'rating 1',
                    value: '2'
                },
                {
                    rating: 'rating 2',
                    value: '0'
                },
                {
                    rating: 'rating 3',
                    value: '5'
                }
            ];

@ajithkrr Post your site’s Editor URL so I can look. Tell me step by step what you did so I and understand what the problem is.

@yisrael-wix Thanks Yisrael …
Here’s the editor URL →
https://editor.wix.com/html/editor/web/renderer/edit/4d5bba74-fe7e-449e-879f-3eef831537fe?metaSiteId=d9153bfa-d740-47c4-8fd4-13e02c41df49&editorSessionId=5e7cb013-96d6-459d-8b7f-28bd3f2c690c&referralInfo=dashboard
(Database is Topic)

  1. First I opened the database (naturally)
  2. Then I selected the field called “Rating” (array field)
  3. Then I pasted, the array I posted above…
  4. Now when I just click on any other field, it is showing the above message…

@ajithkrr I see what you’re talking about. I also tried my test database again and get the same thing. I think the issue is that the array (and object) fields are meant to be used by the API and not edited directly in the Content Manager. The following is from the documentation about the array field:

Allows turning a string into an “array” data type in JavaScript while working with a collection via API.

I would suggest asking Wix Customer Care about this. Meanwhile, I’ll try to found out more about this myself.

I discussed this with Shachar, another member of the team, and she said she clicked on “convert” and it worked for her. It seems that the array field needs a certain format, and if it doesn’t find it, it will ask you if it’s OK to convert to the format that it finds acceptable.

Very very thanks Yisrael…
After you said, i just clicked on the convert and it changed the format…
Even though the code…

wixData.query("Topic")
        .isNotEmpty("rating")
        .find()
        .then((results) => {
 let items = results.items[0];
 let arr = items.rating
            console.log("arr " + arr)
 let obj = arr.find(o => o.rating === 'rating 1');
 let rating1 = obj;
            console.log(obj);

 let obj2 = arr.find(o => o.rating === 'rating 2');
 let rating2 = obj2.value;
            console.log(rating2);

 let obj3 = arr.find(o => o.rating === 'rating 3');
 let rating3 = obj3.value;
            console.log(rating3)

 let obj4 = arr.find(o => o.rating === 'rating 4');
 let rating4 = obj4.value;
            console.log(rating4);

 let obj5 = arr.find(o => o.rating === 'rating 5');
 let rating5 = obj5.value;
            console.log(rating5);
        });

…is not working.

Thanks!!!

@ajithkrr Don’t forget, that if you don’t have a rating in the array, then the obj will be undefined . Before you use obj , you should make sure that it has a value.

@yisrael-wix Surely I have the content.
That’s why is used the isNotEmpty() function…
( A new error is coming )

wixData.query("Topic")
        .isNotEmpty("rating")
        .find()
        .then((results) => {
 let items = results.items[0];
 let arr = items.rating
 console.log("arr " + arr)
 

Console →
(Working properly)

But… →


If I convert the field, this error will come →

If I don’t convert the field, this error will come →

@ajithkrr I see what the problem is. An array can’t be entered into an array field from the Content Manager. The format is incorrect.

You can save an array to a field, and get the array from the field, using the wix-data API in Corvid.

I will try to find out more about this from the data team.

You can enter it manually, but you need to use json format i.e.:

  1. to use double-quote and not a single quote.

  2. to use quotes for the keys as well
    Like:

[{"rating": "rating 1", "value": "2"},{"rating":"rating 2","value": "0"}]

Hi all,

the value in Object and Array fields has to be a valid JSON, entering anything else would result in saving a string.

While JavaScript does not require putting field names in quotes, (quite misleadingly) JSON does.

So you should enter JSON arrays as

[
  { "rating": "rating 1", "value": 1 },
  { "rating": "rating 2", "value": 2 }
]

Hope this helps.

Thanks you !!!
It worked perfectly…
This, JSON, didn’t crossed my mind…
Thanks @yisrael-wix @J. D.

@jonatandor35 Worked perfectly !!!

@jonatandor35 Giedrius from Wix Data told me about this last night.

Are double quotes required in json format? From what I understand the issue was that no quotes at all were being used:

  rating: 'rating 1',

My understanding was that quotes (single or double, doesn’t matter) are required for json format.

Anyhow, glad you spotted this and posted. Nice catch.