I have read through all the ref on hooks and im still haveing trouble getting started, if somebody could show me an example of how my coding would look if i wanted to update one database to another. for example
data base 1 = In one data base i have a list of dogs which keeps track of by there Reg. Name
database 2 = In my second Data base keeps track of how they finshed each event.
I would like to return points from Data base 2 after the information is intered to Data base 1 in the point totals.
any help as far is how the coding would look would be great.
Thanks
Hi James,
On the Database collection view, add a Hook to the first data collection (after insert).
Second on the hook file (in backend library) try to implement this pseudo code:
Please pay attention to the comments
import wixData from 'wix-data';
export function DatabaseCollection1_afterInsert(item, context) { // this function is added automatically after a hook is being added in database collection view
//TODO: write your code here...
let yourItemFromCollection2;
wixData.query("DatabaseCollection2")
.find()
.then((results) => { //
yourItemFromCollection2 = results.items[0]; // the first item was randomly chosen for this example
})
.then(() => {
yourItemFromCollection2.myFieldKey = `I want to update this data with the item from database collection1 ${item.someField}`; // item is the new Inserted from database collection 1
wixData.save("DatabaseCollection2", yourItem)
.then((results) => {
let yourNewItem = results; //see item below
return item; // DatabaseCollection1_afterInsert function expected to return a item
});
});
}
Good luck!
Roi
Hi Roi
Thanks for your help still working on getting it to work. Should the database that if getting the informations from be used as the export database?
Hi Roi
I have the two database one is my doglist and the other is my trail report. The Trail Report is the data base that is being updated that i want to promt the dog list data to updated. Or Should i have a third data base that pulls the information im looking from each Data base? example below
doglist data base
dogname handler points
Sam J. Smith field to update from trial report
Trail Report
Place Date 1st place handler score points
huntclub 1/1/18 Sam J.Smith 1100 3
Hi Kisner,
It’s ok to use two databases, consider adding a unique identity field.
What if you have two dogs named sam ?
Moreover, it would be much simpler with this code.
Check this out:
export function TrailReport_afterInsert(item, context) { // this function is added automatically after a hook is being added in database collection view
//TODO: write your code here...
let yourDogFromDoglist;
wixData.query("doglist")
.find()
.then((results) => {
yourDogFromDoglist = results.items[?] // add here some functionality to find you dog (hint: use the unique identity)
yourDogFromDoglist.points = yourDogFromDoglist.points + item.points;
wixData.save("doglist",yourDogFromDoglist)
.then((results)=> {
let updatedDogFromDogList = results;
})
})
}
Let me know how it goes,
Roi
Roi,
The code above gave alot of triangles and red circle off to the left of the code, not sure if im doing something wrong… also not sure what peramiter not used means?
Can you share a screenshot of the errors ?
Roi
Hi,
What we see here is warning and not error. In line 11 is unread warning (unused variable) and in lines 13-14 a semi-column is missing.
These warnings doesn’t effect the functionality.
Roi
HI Roi,
I would like to do a similar thing but for all fields of the collections… So record same informations into 2 differnents database at same time. From now I’m trying to achieve by Exporting Json from DB1(only single line overwritten record(update function), and Importing (to Archive temporary record from DB1) Json into DB2(many lines record). The problem is the ID field coming from DB1 is always the same. So I thing a hook can solve my problem, but I have any idea wich form this code should have ? Like kisner… with a code exemple I can do it, I’m a beginner in coding but I performed some of them on my web site with API tutorials… here’s the URL if it can help : https://www.cibledetection.com/copy-of-teams-arena-entry
Thanks in advance for any informations about my problem…
Hi Ron,
When using wixData.insert a new _id is created automatically. Why do you need to create this id manually ?
Here is an example of how this hook looks like
Good luck
Roi
Hi Roi, (I have delete last post and republish because I have update too many times) Here’s again!
This ID coming from the DB1 I want to ARCHIVE in a second DB. But DB1 Has to be only a one line RECORD (update ( ) function). I need this one to be compatible with our own software line-up recognition. So once created in DB1 (USER entries) User have to Copy this single line JSON code and paste it into our software. Then this Line-up before create new one into DB1 and erase the last one created, we want to ARCHIVE into a second DB to web display all recorded/builded Line-ups from DB1.
So This is our problem, the _id created into DB1 is always the same, so at Import into DB2 (wathever if it is a insert ( ) function) it will not be saved because it is the Same ID… I don’t want to create/modify this ID manually, this is a temporary solution, before solving this problem… I want a hook to create one automatically! The way I think to perform is to make ID field coming from DB1 blank, so it will create a unique ID field at Import into DB2. Does it make sense !? I cant’s see the hook exemple in your answer 
thanks
-Ronny
PS. A other solutions (asked also on other post) is to always increase default _id value by one at Import ( function beforeInsert or beforeGet)… I tried to make it work without succes! The only result I had is when adding the code into DB insert hook code. I create value ‘‘NaN’’ in the _id field ???
thanks rio still isnt working for me, this is so grrrrrrrr lol
Hi Kisner,
Please share a link to your URL.
Roi
For me it’s working!!! Thanks to SAM for Help, the only thing I had to do from my side is to delete item._id; of DB2 before insert hook!