Hi everyone !
Here is the one question that buzzed my mind →
While going through the afterInsert api, I found out that this function returns the userId of the user who inserted the data to database…
// In data.js
export function myCollection_afterInsert(item, context) {
let hookContext = context;
return item;
}
/*
* hookContext:
*
* {
* "collectionName": "myCollection",
* "userId": "f45jf8d2-grkj-2opd-4ovk-9rfj4wo5tvj3",
* "userRole": "siteOwner"
* }
As this is in a back-end file, can I institute an if-condition in the front-end .
Something like this
front-end →
if(currentUserId === returnedUserId) {
$w('#dataset1').refresh();
}
Is this possible ???
Hi @ajithkrr 
Hook functions do not return values to frontend, just check the credentials before inserting/saving the data, it’s the best practice.
@ahmadnasriya What I am trying to do is to update the dataset on the client side and I figured out the latter can be achieved by using . jsw file…
Database fields are userId, recieverId, value
When one user inserts value into the database, the hook or any should check if the current user id matches the recieverId (inserted in the database with value) then the dataset should refresh…
Is this possible???
@ajithkrr Yes it’s possible to update a particular item(s) on the backend, but only through wixData API, Datasets are frontend components and only work on the client side.
@ahmadnasriya Thanks for your answer…
But I am not trying to update value in the database…instead
After the value is inserted (values → userId of who inserted and a image ) to the Database… the dataset,
of those users who are currently viewing the site, should refresh…
Just the best example is this community discussion page itself…
Is this possible ???
@ajithkrr Do you want to refresh the dataset after inserting an item on the backend? OK, is the backend function being called from the client side? If not then it’s not possible, if yes then observe the following:
import {insertData} from 'backend/module.jsw';
export async function button_click(() => {
await insertData().then(() => {
$w('#dataset').refresh();
})
})
But I don’t understand why would you want to insert data on the backend instead of using the dataset to submit the data.
Hope this helps~!
Ahmad
@ahmadnasriya Thanks for your answers…
I am not using any backend function till now (if needed I will). Also I am inserting value to database in the front end. What my query is when an user inserts any image to the database, other users 's , who are online or viewing the site, dataset should refresh…
For instance, when I asked one question (in this Corvid Disscusion page) in a post, I got the answer in that instance without me refreshing the page…
Thanks for your answers…
@ajithkrr That’s not possible unfortunately. Unless you work with the Real Time APIs .
@ahmadnasriya One more…
Can you post or give me a clue about what is done here…
It is shown that this page is made by Corvid…
So , on another post, when you were typing the answer, it showed that ‘Ahmad is typing’
Could I achieve the latter using Corvid??
Thanks…
@ajithkrr Yes, you can achieve all of that with the Real Time API.
@ahmadnasriya With the RealTime API, Just can you give me an example of how to show a box, in the page of all users who are viewing the page, when the database values are changed or after inserting the values in the database by someone…
Thanks !!!
@ajithkrr Hi, I’m sorry I’m unable to do that at the moment.
@ahmadnasriya No problem…
But can you tell - should I be here or create a new post on ‘RealTime Api after insert’
Thank You !!!
@ajithkrr definitely a new post 