Move a value from one collection to another with a hook

All,

I’m trying to return the matching rarity of a hero by querying the hero name and move that with the data that was submitted by an user to the user hero data database.
Databases and fields involved:
Master_Hero_Data
- Hero_Name
- Rarity
User_Hero_Data
- Hero_Name
- Rarity

The users will be filling out a form with their hero’s name and this is what I’m trying to use to query the Master Hero Data collection for the matching rarity. I’ve also been using code to copy the Item ID to a field in the User Hero Data as the Title so it can be a unique key for the User Hero Data collection. Also there are some items below that are calculating various effectiveness of some hero abilities with the entered attack, health, and defense but those are all functioning.

My code is listed below, can anyone point me in the direction to see why the rarity is not being copied over?

import wixData from ‘wix-data’;

export function User_Hero_Data_afterInsert(item, context) {
let toUpdate = item;
let newTitle = item._id;
let heroName = item._hero_name;
let curTile = 26*(Math.pow((item.cur_attack/830),1.35));
let curTank = (item.cur_health/(100.83*(Math.pow((815/item.cur_defense),1.25))));
toUpdate.cur_tank_score = curTank;
toUpdate.cur_tile_score = curTile;
toUpdate.title = newTitle;

wixData.query(“Master_Hero_Data”)
.eq(“heroName”, heroName)
.find()
.then ( (results) => {
let items = results.items;
let firstItem = items[0];
let newRarity = items.rarity;
toUpdate.rarity = newRarity;
})
. catch ( (error) => {
let errorMsg = error.message;
let code = error.code;
});

wixData.update(“User_Hero_Data”, toUpdate)
.then( (results) => {
let item = results;
} )
. catch ( (err) => {
let errorMsg = err;
} );
return item;

}

Hi,

did you try to add some console.log in your code and see what your code actually does? you can see them during preview in your wix console logs

Shlomi

I didn’t know that was a possibility. I’ll research it now. I appreciate the response!

Ok, I’m still stuck on this. I’ve tried using the logging but it’s not providing me with any help. I did figure out that I had one variable that was incorrect but the below code still does not populate the “Rarity” field with the correct value. I’m confident the problem is with my query code. I set a temporary variable value of 1 for rarity, but it should update to a larger number when the user submits the form.

import wixData from ‘wix-data’;
console.log(wixData);

export function User_Hero_Data_beforeInsert(item, context) {
let toUpdate = item;
let newTitle = item._id;
let heroName = item.hero_name;
let curTile = 26*(Math.pow((item.cur_attack/830),1.35));
let curTank = (item.cur_health/(100.83*(Math.pow((815/item.cur_defense),1.25))));
let newRarity = 1;
toUpdate.cur_tank_score = curTank;
toUpdate.cur_tile_score = curTile;
toUpdate.title = newTitle;
toUpdate.rarity = newRarity;

wixData.query(“Master_Hero_Data”)
.eq(“hero_name”, heroName)
.find()
.then ((results) => {
let items = results.rarity;
let firstItem = items[0];
let newRarity = items.rarity;
})
. catch ( (error) => {
let errorMsg = error.message;
let code = error.code;
})

wixData.update(“User_Hero_Data”, toUpdate)
.then( (results) => {
let items = results;
} )
. catch ( (err) => {
let errorMsg = err;
} );
return item;
}

what id do you use for the update item operation?
toUpdate takes the id from the User_Hero_Data?
also the following does not make sense as it is an array:
let newRarity = items.rarity;

unfortunately we can not help you debug your code, i’d recommend you try to simplify it first, maybe also move it to the page code just to understand what you are missing, or even try in the data hook to just update a user hero item by its id just to see it is working

I’m providing an update so that anyone who stumbles upon this thread in the future can hopefully gain something from the headaches and stress this issue caused me as a non-programmer. The best course I found to accomplish this was to create a third dataset on the page that holds the form. The third dataset is a copy of the master data collection’s dataset and is filtered by the primary key (in my case hero name). Once I had this in place I used the code below in the “on click” function of my submit button. This may be a bit too broken down for more advanced users but I’m hoping it helps those like myself with no coding experience. Obviously you would remove the comments that explain what each line does.

export function button1_click(event, $w) {
let heroRarity = $w(‘#dataset3’).getCurrentItem().rarity;
//dataset 3 is my copy of my main collection’s dataset filtered by hero name
$w(‘#dataset1’).setFieldValue(“rarity”, heroRarity);
//dataset 1 is my user data collection where I want the data copied to. “rarity” is the Field Key of my field
//and heroRarity is the variable I defined above to hold the value

}