I’m using a code I found on the forum:
Backend
import wixCrm from 'wix-crm-backend';
export function notifyOwnerOnDashboard(title, action,site,role) {
wixCrm.notifications.notify(
"Notification body",
["Dashboard"],
{
"title" : title,
"actionTitle": action,
"actionTarget": {"url": site},
"recipients": { "role": role}
}
);
console.log('Notification sent')
}
In page
import {notifyOwnerOnDashboard} from 'backend/Notification';
export function Notifyteam() {
const title = "I'm a title"
const action = "This is what I have done" ;
const site = "www.google.com" ; // use full URL link
const role = "Owner";
notifyOwnerOnDashboard(title,action,site,role)
}
export function btnNotification_click(event) {
Notifyteam()
}
but copying it exactly the same gives me this error:
How can i fix this?
Please realize that you can’t just “copy and paste” code and expect it to work. You need to understand what’s happening if you expect to be able to write code that does what you want.
For starters, you will need to read about Corvid Web Modules: Calling Server-Side Code from the Front-End . After that, you will need to follow the wix-crm-backend API .
If I had figured out how to fix I wouldn’t have written the post don’t you think? I wrote this post just to understand at least where the error is, since the server replies to me 400 and the console does not report any errors in the code. Those links you brought back I have already visited but nothing done.
Could you please help me understand the reason for this server response?
@bigimatt14 Yes, you are right, but just copying and pasting is the answer.
“To at least understand where the problem is”, you will need to read Corvid Web Modules: Calling Server-Side Code from the Front-End . Web modules (backend functions) return a Promise that needs to be handled, and your code is not doing that. For more information about Promises, see the following articles:
@yisrael-wix Do you mean it this way?
Backend
import {notifications} from 'wix-crm-backend';
export function notifyOwnerOnDashboard(title,action,site,role) {
console.log('backend');
notifications.notify(
"Notification body",
["Dashboard"],
{
"title": title,
"actionTitle": action,
"actionTarget": {"url": site},
"recipients": {"role": role}
}
).then(function(response){
if (response.status >= 200 && response.status < 300) {
return response.text();
}
else{throw new Error(response.statusText);}
})
}
Because even so it gives me the same error
I tried an example on my own and it works fine. Do you have your backend code in a .jsw file?
Here’s what I did…
Page code:
import { notifyOwnerOnDashboard } from 'backend/notify';
$w.onReady(function () {
$w("#btnNotify").onClick((event) => {
notifyOwner();
});
});
export function notifyOwner() {
const title = "notification title";
const action = "action title"
const site = "https://www.wix.com";
const role = "Owner";
notifyOwnerOnDashboard(title, action, site, role);
}
Web module (backend code):
// Filename: backend/notify.jsw (web modules need to have a .jsw extension)
import {notifications} from 'wix-crm-backend';
export function notifyOwnerOnDashboard(title, actionTitle, actionTarget, role) {
notifications.notify(
"Notification body",
["Dashboard"],
{
"title": title,
"actionTitle": actionTitle,
"actionTarget": {"url": actionTarget},
"recipients": {"role": role}
}
);
}
I hope this helps.
Now it’s working, I still don’t understand why before it gave me error 400 being the same code haha.
Thanks for the help!