Read a single data from database

i want to read a single int from my database. I know how i read all informations of my database with this code:
wixData . query ( “punkte” )
. eq ( “name” , name )
. find ()
. then ( ( results1 ) => {
console . log ( results1 );
} );

but the output is this: “{”_items":[{“name”:“team1”, “punkte”:0 ,“_id”:“2145340a-f80b-4807-a2b8-db9683809371”,“_createdDate”:“Thu Sep 23 2021 19:08:53 GMT+0200 (Central European Summer Time)”,“_updatedDate”:“Thu Sep 23 2021 19:08:53 GMT+0200 (Central European Summer Time)”,“password”:“pw1”,“link-accountdaten-1-name”:“/accountdaten-1/team1”,“link-accountdaten-name”:“/accountdaten/team1”,“link-accountdaten-2-name”:“/681091/”}],“_totalCount”:1,“_query”:{“invalidArguments”:[],“filterTree”:{“$and”:[{“name”:“team1”}]},“provider”:{“save”:“function e(){return t.getJSON("/wix/data-web.jsw/save.ajax",arguments)}”,“insert”:“function e(){return t.getJSON("/wix/data-web.jsw/insert.ajax",arguments)}”,“update”:“function e(){return t.getJSON("/wix/data-web.jsw/update.ajax",arguments)}”,“get”:“function e(){return t.getJSON("/wix/data-web.jsw/get.ajax",arguments)}”,“distinct”:“function e(){return t.getJSON("/wix/data-web.jsw/distinct.ajax",arguments)}”,“truncate”:“function e(){return t.getJSON("/wix/data-web.jsw/truncate.ajax",arguments)}”,“remove”:“function e(){return t.getJSON("/wix/data-web.jsw/remove.ajax",arguments)}”,“find”:“function e(){return t.getJSON("/wix/data-web.jsw/find.ajax",arguments)}”,“runAggregate”:“function e(){return t.getJSON("/wix/data-web.jsw/runAggregate.ajax",arguments)}”,“count”:“function e(){return t.getJSON("/wix/data-web.jsw/count.ajax",arguments)}”,“bulkInsert”:“function e(){return t.getJSON("/wix/data-web.jsw/bulkInsert.ajax",arguments)}”,“bulkSave”:“function e(){return t.getJSON("/wix/data-web.jsw/bulkSave.ajax",arguments)}”,“bulkUpdate”:“function e(){return t.getJSON("/wix/data-web.jsw/bulkUpdate.ajax",arguments)}”,“bulkRemove”:“function e(){return t.getJSON("/wix/data-web.jsw/bulkRemove.ajax",arguments)}”,“queryReferenced”:“function e(){return t.getJSON("/wix/data-web.jsw/queryReferenced.ajax",arguments)}”,“insertReference”:“function e(){return t.getJSON("/wix/data-web.jsw/insertReference.ajax",arguments)}”,“insertReferences”:“function e(){return t.getJSON("/wix/data-web.jsw/insertReferences.ajax",arguments)}”,“replaceReferences”:“function e(){return t.getJSON("/wix/data-web.jsw/replaceReferences.ajax",arguments)}”,“removeReference”:“function e(){return t.getJSON("/wix/data-web.jsw/removeReference.ajax",arguments)}”,“removeReferences”:“function e(){return t.getJSON("/wix/data-web.jsw/removeReferences.ajax",arguments)}”,“isReferenced”:“function e(){return t.getJSON("/wix/data-web.jsw/isReferenced.ajax",arguments)}”,“getSchema”:“function e(){return t.getJSON("/wix/data-web.jsw/getSchema.ajax",arguments)}”},“collectionName”:“punkte”,“limitNumber”:50,“skipNumber”:0,“included”:[]},“_pagingFunction”:“pagingFunction(){return this.find(i)}”,“_hasExtraItem”:false,“nextSkipNumber”:1,“prevSkipNumber”:-50}"

I just want a the integer from “punkte”:0. How can I get this int?

Projections are not yet possible in wixData. There are 2 solutions:

  1. use the sql-package. That one lets you do “select fieldname from tablename”
  2. add a bit of code after the query to loop through the result, put the field (maybe + _id) into an array and return that.

if i connect a text to a dataset can i read out the text field?

Yes, but the data you showed above will still be visible inside the brower’s Network tab. More info here: https://www.wix.com/velo/forum/tips-tutorials-examples/giri-gives-a-fac-4-we-can-see-your-data-set

I have had to do something like that too. I didn’t want all the fields returned.
So I just created a single record with the single field and returned it. Here is something similar that I did.

async function getPunkte(name) {
    return wixData.query("punkte")
        .eq("name", name)
        .find()
        .then( (results1) => {
            var result = [{"punkte": 0}]; // default value = (not found/more than one)
            var numofrecs = results1.items.length;
            if (numofrecs ==1) {
                // there should only be one record
                result = [{"punkte": results1.items[0].punkte}];
                console.log(result);
            } else if (numofrecs ==0) {
                console.log("Invalid number of records found: [No records exist].");
            } else {
                console.log("Invalid number of records found: [More than 1 record exist].");
            }
            return result;
        })
        .catch( (err) => {
            console.log(err);
        });
}


Hi there,

An easy way to return only what you need is to have an array of your required fields that you want to return, then loop through your items and create a new array based on the required fields.

For example, when querying the collection " members ", you get the following schema:

const member = {
    _id: "<some ID">,
    name: 'John Doe',
    age: 29,
    email: 'email@domain.com',
    password: "<a hash value of the password">,
    phone: "+970-XXXXXXXXXX",
    picture: "https://...../pic.jpg'
}

And you only want to return the name and the picture of your members, so we create an array that includes the IDs we want to copy, then we loop through the member’s array, and have another nested loop to loop through the required fields’ array, finally, we push the new member into the new array.

const req = ['name', 'picture'];
const modified_members = [];
const members = await query('members').find().then(x => { return x.items });

// Now loop through the members array
for (const member of members) {
    const new_member = {} // An object for the new member
    
    // Now loop through the required fields
    for (const field of req) {
        if (member[field]) { new_member[field] = member[field] }
    }
    
    if (Object.keys(new_member).length > 0) { modified_members.push(new_member) }    
}

return modified_members;

Hope this helps~!
Ahmad

Also erstens, ich würde keine Passwörter in einer query verarbeiteten und auch nicht so offensichtlich und unsicher in einer Datenbank ablegen.

Ok. After you have your results of your query, add the following code-part into → then()-part of your code…
.then((res)=>{
let firstItem=res.items[0].punkte
console. log(firstItem);
});

Take a look into console.