Cannot save value to dataset

I am trying to set a field value and save it to the data collection. the field (FK_Type_Id) is set before the save and then is loses it’s value and doesn’t get passed into the saved record… see console output below. What am I doing wrong? I have tried everything.

[itemValuesChanged event] triggered on wix-dataset with ( {" recordBeforeChange “:{“prayerDate”:“2018-07-26T04:00:00.000Z”,“description”:“h”,“title”:“h”,“isPublic”:true,”_id":“524526c5-b162-4bab-a632-72b72d0bdc2c”,“User_Userid”:“0bc89b37-4386-4b83-bcbc-3c91f0cf3e6e”," FK_Type_Id":“ab1e9ffb-37b3-474d-a92d-ab298945beb8” }," recordAfterChange “:{“prayerDate”:“2018-07-26T04:00:00.000Z”,“description”:“h”,“title”:“h”,“isPublic”:true,”_id":“524526c5-b162-4bab-a632-72b72d0bdc2c”,“User_Userid”:“0bc89b37-4386-4b83-bcbc-3c91f0cf3e6e”,“_owner”:“0bc89b37-4386-4b83-bcbc-3c91f0cf3e6e”,“_createdDate”:“2018-07-26T19:25:42.115Z”,“_updatedDate”:“2018-07-26T19:25:42.115Z”}} )
console.js:35 [afterSave event] triggered on wix-dataset with ( {" beforeSaveRecord “:{“prayerDate”:“2018-07-26T04:00:00.000Z”,“description”:“h”,“title”:“h”,“isPublic”:true,”_id":“524526c5-b162-4bab-a632-72b72d0bdc2c”,“User_Userid”:“0bc89b37-4386-4b83-bcbc-3c91f0cf3e6e”,“_owner”:“0bc89b37-4386-4b83-bcbc-3c91f0cf3e6e”,“_createdDate”:“2018-07-26T19:25:42.115Z”,“_updatedDate”:“2018-07-26T19:25:42.115Z”}," afterSaveRecord “:{“prayerDate”:“2018-07-26T04:00:00.000Z”,“description”:“h”,“title”:“h”,“isPublic”:true,”_id":“524526c5-b162-4bab-a632-72b72d0bdc2c”,“User_Userid”:“0bc89b37-4386-4b83-bcbc-3c91f0cf3e6e”,“_owner”:“0bc89b37-4386-4b83-bcbc-3c91f0cf3e6e”,“_createdDate”:“2018-07-26T19:25:42.115Z”,“_updatedDate”:“2018-07-26T19:25:42.115Z”}} )
console.js:35 [wix-dataset.save] returned with ( {“prayerDate”:“2018-07-26T04:00:00.000Z”,“description”:“h”,“title”:“h”,“isPublic”:true,“_id”:“524526c5-b162-4bab-a632-72b72d0bdc2c”,“User_Userid”:“0bc89b37-4386-4b83-bcbc-3c91f0cf3e6e”,“_owner”:“0bc89b37-4386-4b83-bcbc-3c91f0cf3e6e”,“_createdDate”:“2018-07-26T19:25:42.115Z”,“_updatedDate”:“2018-07-26T19:25:42.115Z”} )

Here is the code:

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

let user = wixUsers.currentUser;
let typeId = “testing”;

export function button1_click(event, $w) {
let typeName = $w(‘#dropdown1’).value;

$w("#dataset2").onReady(() => { 

    console.log("dataset is onReady..."); 

    console.log('typeName: ' + typeName); 

    wixData.query("Type") 
        .eq('name', typeName) 
        .find() // Run the query 
        .then(results => { 

let items = results.items;
let firstItem = items[0];
typeId = firstItem[‘_id’];

            console.log('results in query function: ' + typeId); 
            setFields(typeId); 

        }). **catch** (error => { 

            console.log(error); 
        }); 

    $w("#dataset2").setFieldValue("User_Userid", user.id); 

function setFields(typeID) {
console.log(“before setFieldValue…” + typeID);
$w(‘#dataset2’).setFieldValue(“FK_Type_Id”, typeID);
}
});

Hi,
Instead of using the setFieldValue, try the update function to update a record with a new value.

Good luck,
Tal.

update function only works with wixData, not dataset. I need to pass the value returned from the query to set the dataset value. The code below works when I hard code the value… but, does not work when I call to the query that holds the value I need.

export function button1_click(event, $w) {
let typeName = $w(‘#dropdown1’).value;

$w("#dataset2").onReady(() => { 

let typeId;

    console.log("dataset is onReady..."); 

    console.log('typeName: ' + typeName); 

    $w("#dataset2").setFieldValues({ 

“UserId”: user.id,
“Type_Id”: “New Name” //This is the value I am trying to set from the query below. But it always returns undefined.
});

}); 

}

export function getTypeId(typeName) {
return wixData.query(“Type”)
.eq(‘name’, typeName)
.find() // Run the query
.then(results => {

let items = results.items;
let firstItem = items[0];
let typeID = firstItem[‘_id’];

        console.log('results in getTypeId function: ' + typeID); //this prints out the value fine.. 
        $w("#dataset2").setFieldValue("FK_Type_Id", typeID); //this will not set the value. 

return items; // this returns undefined

    }). **catch** (error => { 

        console.log(error); 
    });

I managed to get this working. I had to move the set field to the
dataset2_itemValuesChanged function/event

export function getTypeId(typeName) {
return wixData.query(“Type”)
.eq(‘name’, typeName)
.find() // Run the query
.then(results => {
return results.items;
}). catch (error => {
console.log(error);
});
}

export function dataset2_itemValuesChanged() {
//Add your code for this event here:
let typeId;
let typeName = $w(‘#dropdown1’).value;

console.log("itemValuesChanged event fired.. "); 
getTypeId(typeName).then(items => { 

let firstItem = items[0];
typeId = firstItem[‘_id’];

        $w("#dataset2").setFieldValues({ 

“user_userid”: user.id,
“type_id”: typeId
});
})

}