Hello,
I’m trying to understand how wix-router works because I do not understand from the guides.
So I created a router called admin. Here I inserted a page called “Events” in addition to the one created automatically called admin-page which is empty.
The routers.js file has been created which contains this as from screen shot:
I tried to access the “Events” page in advance, but it gives me an error like a sceen shot:
I also tried online and this is the mistake
I tried to edit routers.js this way, but the error remains
I read these articles to understand something, but I can not manage to get them.
Someone can help me by giving me some advice, I have other examples to observe, because I would like to learn this function that I find useful.
Thank you
When creating a router, you choose which requests will get handled by the router based on a URL prefix that you specify, so in your case, admin router creates a new URL on your site - /admin . Requests that include this prefix will be directed and handled by your router.
When you add this router, you need to specify what page will be returned - the errors that you get seem to indicate that there’s no corresponding page to be displayed once next() is called. You should either add a page with the same name or use ok() and specify the page to redirect to.
Thanks for your answer,
efectively with ok () it works.
I went further to try to understand, and looking at examples of code I tried to set this up.
The situation and this that I tried to create:
I have two pages created within the admin router an event call and a user call. These two pages can only be accessed by administrators and not by registered users as normal.
import {ok, forbidden, notFound, WixRouterSitemapEntry} from "wix-router";
import wixUsers from 'wix-users';
import wixWindow from 'wix-window';
const pageAdmin = {
"eventi": {
title: "Gestione Eventi",
url: "https://www.danielphoto.it/admin/eventi"
},
"Utenti": {
title: "Gestione Utenti",
url: "https://www.danielphoto.it/admin/utenti"
}
};
export function admin_Router(request) {
if (request.path[0] === "admin") { //check if the address of the router has been entered
if (request.user) { //check if the user is logged in
if (request.user.role === "siteOwner") { //check if the user is an administrator
const sezione = request.path[1]; //I'll take the name of the page you're looking for es. www.mysite.com/admin/eventi
const data = pageAdmin[sezione];
if (data) {
const seoData = {
title: data.title,
noIndex: false,
};
return ok("admin", data, seoData);
} else {
return notFound();
}
} else { // if the user not is an administrator open lightbox allert
wixWindow.openLightbox("NotUserAdmin");
}
} else { //if the user is not logged in open prompt login
wixUsers.promptLogin({"mode": "login","lang": "it"});
}
} else {
return forbidden();
}
}
export function admin_SiteMap() {
const siteMapEntries = Object.keys(pageAdmin).map( (sezione) => {
const data = pageAdmin[sezione];
const entry = new WixRouterSitemapEntry(sezione);
entry.pageName = "admin"; // The name of the page in the Wix Editor to render
entry.url = `/admin/${sezione}`; // Relative URL of the page
entry.title = data.title; // For better SEO - Help Google
return entry;
});
return siteMapEntries;
}
Does not work
You would help me by suggesting other guides to understand the operation of the router or helping me with the written code, so as to understand where I was wrong
Thank you
redDany did you solve ? And how ?
Hi Mauro,
no I have not solved, these days I had little time to try to find a solution, and I was hoping for someone who could help me.
If I can get to the solution, I would definitely share it
Thanks RedDany, today I’m not able to get it to work, I hope that router.js 'll work and that’s only problem of poor references.