In the docs you have an example of using this to get the query vars.
import wixLocation from 'wix-location';
// ...
let query = wixLocation.query; // {"species": "african-elephant"}
So if I want to extract the value from species how do I do that?
let species = query[“species”];
or
let species = query[“0”];
Any ideas?
you get a plain old json object.
so you can just use query.species to get the value, just like any other js property.
So in a router that would be used as
let subject = request.query.species;
And that would correspond to ?species=giraff
And then the value of subject would be giraff?
I can’t get value from this url : https://www.wixsweden.se/api/?subject=wixdemo
The router is called api router.
the code in routers.js file is
export function api_Router(request) {
// Get the data from query
let subject = request.query.subject;
console.log(subject);
if (subject === "wixdemo") {
//define SEO tags
const seoData = {
title: subject,
description: "This is a description of " + subject + " page",
noIndex: false,
metaTags: {
"og:title": subject,
}
};
}
// Return 404 if item is not found
return notFound();
}
It still throws the return notFound() at the bottom, any ideas? It seem straight forward.
Ok, continues to bother me and I continue to bother you all. But in the end you will be happy of the demo site I am creating.
The let = subject does not get any value but the below code gets the values.
export function api_Router(request) {
let item = null;
if (request.query.subject.includes("wixdemo")) {
const seoData = {
title: "WIX Sweden Demo - " + request.query.subject,
description: "This is a description of " + request.query.subject + " page",
noIndex: false,
metaTags: {
"og:title": request.query.subject,
}
};
let toInsert = {
"title": request.query.subject
};
wixData.insert("emails", toInsert)
.then( (results) => {
item = results; //see item below
// Render item page
console.log(item);
});
return ok("api-page", item, seoData);
}
// Return 404 if item is not found
return notFound();
}
Now the page renders but I do not get any data inserted in my data collection. Can someone help?
So it all works, if you are not stupid like I am you will check data collection permissions before you bother other people with your shitty problems.
The code above works… Now I will write the showcase article.