"_owner" field in database not filled properly

Hi

When adding data to the database, the “_owner” field does not get filled properly.
A whole bunch of data is sended to the database.
when going to a specific page in my account, people see this data in a table
but the data is pulled for them specific (site autor only).
now when the “_owner” field is not filled they only see part of (4 items are filled more then 10 are not filled)

How it should be:

what they actualy see…

So my question,
why is “_owner” not getting filled with the right data?
my code is olmost the same for each item.

kind regards,
Kristof.

2 scenarios:

  1. If the user is not a site member or admin and the database permissions for ‘Write’ is set to ‘Anyone’ then there will be no ‘_owner’ value

  2. If you insert the data from a backend file while suppressing authorization

What code are you using?

  1. The user is a site member, they can’t enter on the website unless they have an account. and the database ‘write’ settings are set to site member.

  2. what do you mean with suppressing authorization?

this is part of the code

front end:

//Guess you mean this with suppressing authorization?
let options = {
 "suppressAuth": true,
 "suppressHooks": true
};

function addOnderdeel(indexNr, sortNr, naam, info, db,korting = 0) {
    
 let toInsert = {
 "bestelNr": bestelNr,
 "referentie": $w("#txtReferentie").value,
 "indexNr": indexNr,
 "sortNr": sortNr,
 "naam": naam,
 "info": info,
 "korting" : korting
    }
    wixData.insert(db, toInsert, options)
}


addOnderdeel(
 i,
 200,
 "kastverbredinglinks",
 data[i].kastverbredinglinks,
 db,user.Meerprijs
)

backend:
This is in my beforeInsert:


 if (item.naam === "kastverbredingrechts") {
 let toinsert = {
 "bestelNr": item.bestelNr,
 "referentie": item.referentie,
 "indexNr": item.indexNr,
 "sortNr": item.sortNr,
 "naam": "Kastverbreding rechts",
 "info": item.info + "mm",
 "prijs": await getExtraMeerprijs("meerprijs", "kastverbreding"),
 "korting": 40

        }
        item = toinsert
    }
 //console.log(item)
 return item

Kind regards,
Kristof

I also just found out that the part where “_owner” is filled, send from the frontend and isn’t used in the backend code(beforeInsert).
But why it works in the frontend and not when it goes trough the backend, i don’t know :sweat_smile:

kind regards,
Kristof.

@volkaertskristof I think this is not mentioned anywhere in the docs but when you insert from frontend as a site member/admin you have a member id which is used for the ’ _owner ’ field (this is auto detected by the wixData API)

When you insert from backend, the system does not know who you are but since DB permissions may allow the insert due to " suppressAuth: true " value or the DB write permissions being ‘Anyone’ you will not have a ‘_owner’ value for the field because like I said - the server does not know who you are

BUT if you manually get the user id using the below

let owner = wixUsers.currentUser.id; 

Then you can send it to your backend to be inserted as the ‘_owner’ field

"_owner": owner

@shantanukumar847 Thanks,
It seemed to work this way.