Insert UserId into a Referenced Field

I am trying to grab current users userid and insert it into a new connection. I have seen several similar discussions on forums but i cant seem to figure out the right way to do this. The reason i need to grab userid vs using owner, is that future other repeaters need to be looking at username so that i can filter based of that. I cant use owner to filter because that forces filter by logged in user. My current code is very basic and inserts fine but with a ‘Broken Reference’ for username. I am a novice when comes to any kind of coding. Thanks in advance for the assistance

import wixUsers from ‘wix-users’ ;
import wixData from ‘wix-data’ ;

let user = wixUsers.currentUser
let userID = user.id

$w.onReady( function () {
});

export function button8_click(event){
let tosave = { ‘username’ : userID}
wixData.insert( “MyMiles” ,tosave)
}

If I get you right, you wish to update an existing record. Right?
Then first get this record usong wixData.query() or wixData.get()
then update the ref field:
record.user = userID;
and wixData.update(CollectionId, record)

Thanks J.D. Not trying to update an existing record. Only when inserting/adding a new record

@bjamaspi So the code you posted should work. Check the followings:

  1. The collection name is correctly spelled.

  2. The filed key is correctly spelled.

  3. The collection permissions allow writing (for everyone/members depends on what you need).