Updating _owner field

I have a user filling out data before signing up. If the user doesn’t sign-up, he still gets some information (thats what we use as a hook to get customers). If the user signs-up, I would like to save that data under that user, so he can return to it.

I implemented all of that, saving the data I want to save in the local storage (by wix).
Upon register, I try to update the _owner field in the db, which is not working properly.

code:

import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
import { local } from 'wix-storage';
import wixData from 'wix-data';
$w.onReady(function () {
$w("#submit").onClick( () => {
let email = $w("#emailInput").value;
let password = $w("#passwordInput").value;
let firstName = $w("#firstNameInput").value;
let lastName = $w("#lastNameInput").value;
console.log(`Registeting user ${firstName} ${lastName}`);
wixUsers.register(email, password, {
contactInfo: {
"firstName": firstName,
"lastName": lastName,
}
} ).then( (result) => {
let currentQuestionnaire = JSON.parse(local.getItem("currentQuestionnaire"));
currentQuestionnaire._owner = result.user.id
console.log(currentQuestionnaire._owner);
if ( currentQuestionnaire !== null ){
console.log("Updatign questionnaire owner")
console.log(currentQuestionnaire);
currentQuestionnaire._owner = result.user.id;
wixData.get("Questionnaire", currentQuestionnaire._id)
.then((item) => {
wixData.update("Questionnaire", currentQuestionnaire);
console.info(`updated questionnaire ${currentQuestionnaire.title}, data: ${currentQuestionnaire}`);
console.log(JSON.stringify(item));
wixLocation.to('/thank-you');
})
.catch((err) => {
let errorMsg = err;
});
} else {
console.log("No questionnaire found in cache");
wixLocation.to("/thank-you");
}
} );
} )
});

Why isnt this working (im guessing that _owner is a private field or something)? and how can I achieve this (I really rather avoid having 2 owner columns in the db)?

Depending on what dataset you are trying to use, then you can’t as it is created automatically.

In collections from Wix apps that get automatically added when you add the Wix app to your site, like Wix Members app and the Private Members Dasta collection, the id field here is the same as the owner field in a dataset that you add yourself.
https://support.wix.com/en/article/corvid-wix-members-privatemembersdata-collection-fields

Permissions
The PrivateMembersData collection has the following permissions :
Read: Site member author
Create: None
Update: None
Delete: None
You cannot change the PrivateMembersData collection permissions.

ID (_id)
Description : The member ID that was created by the server. This is a system field and is hidden by default.
Type : Text
Can connect to data : Yes
Can use in dynamic page URL : Yes
Read-only : Yes

In a dataset that you add yourself to your website has the owner field, which is the same as the id field in Wix app collections, as well as the id field which you can set otherwise it will get automatically added and you can’t change it after.
https://support.wix.com/en/article/about-your-database-collection-fields#system-fields

System Fields
Every database collection contains the default fields shown here.
They cannot be edited and are hidden by default.

Field Name
ID
Field Key
_id
Field Type
Text
Description
A unique identifier for the item. You can assign the ID a value when you import new data from a CSV file. Otherwise the ID is a random UUID.
Using Corvid by Wix you can also assign the ID a value when adding items with the Data API.
Once defined the ID cannot be edited.

Field Name
Owner
Field Key
_owner
Field Type
Text
Description
A unique identifier for the creator of the item. Used by the permissions model to determine which user is assigned the Site member author role for the item.

As for your website users signing themselves up to your site or not, then please note that there are two different API for these options.

As noted in the Wix CRM API reference…

What’s the difference between wix-crm and wix-users?
The CRM API contains functionality for working with your site’s contacts. The Users API contains functionality for working with users who are logged-in members. Note that all members are also contacts, but contacts are not necessarily members.

When users sign up to your site and become site members, you then use the Wix Users API and you can see more about that here.
https://www.wix.com/corvid/reference/wix-users.html
https://www.wix.com/corvid/reference/wix-users-backend.html

Please note too, that Wix Users API will only work fully when being used in a live published site, so you might get parts that don’t work if you simply test it through preview mode.

However, if the website user does not signup to your site, then they are classed as a contact and you need to be using the Wix CRM with them.
https://www.wix.com/corvid/reference/wix-crm.html
https://www.wix.com/corvid/reference/wix-crm-backend.html

Also, you are missing the results line too, this is for a lightbox signup on a site of mine.

import wixUsers from 'wix-users';
import wixWindow from 'wix-window';
import wixLocation from 'wix-location';

$w.onReady(function () {
    
    $w("#registerButton").onClick( (event) => {
        
   let email = $w("#email").value;
   let password = $w("#password").value;
   let first = $w("#firstName").value;
   let last = $w("#lastName").value;

   wixUsers.register(email, password, {
       contactInfo: {
        "firstName": $w('#firstName').value,
        "lastName": $w('#lastName').value,
       }
      } )
      .then( (result) => {
        let resultStatus = result.status;
  wixWindow.lightbox.close();
  wixLocation.to("/sign-in-status");  //Change the URL ending to whatever page you want to send the user to after they log in.
      } );     
    } );
});

Sorry Rafael, my apologies, I completely forgot about an old reply in a previous forum post from Povilas (Wix Mod) that states you can now do it by using Wix Data Options.
https://www.wix.com/corvid/forum/community-discussion/changing-the-owner/p-1/dl-5d4aaf8421f60b0017324951

Many thanks go to @giri-zano for reminding me about this, so make sure that any thanks go to them and not myself. :+1:

@giri-zano Thanks a lot, @givemeawhisky also thank you. But now this works in preview mode and not in published mode (these differences are killing me!)

sandbox flow succeeded:

live didnt update owner:

@givemeawhisky @yisrael-wix could this be permission issue? I know that the flow is right because it works in preview, not sure how to debug this further