This is the first time I use axios for queries … but now I don’t know any further, I hope someone can give me an advice.
In order to develop a dictionary app with React Native, I want to query wiktionary like this
let url = "https://en.wiktionary.org/w/api.php?format=json&action=query&titles={word}&rvprop=content&prop=revisions&redirects=1&callback=?".replace("{word}", word);
...
axios({
method: 'get',
url: url,
}).then((response) => {
var results = {
title: "",
definitions: [],
examples: []
}
....
let data = response.data;
...
This query itself works … now I would like to adapt this for my purposes: wiktionary-parser.
The problem occurs here:
if(!data || !data.query || !data.query.pages || data.query.pages[-1]) {
return callback({});
}
It says
TypeError: Cannot read property 'pages' of undefined
The way the data from my query are organized must differ from the data received by this "$.getJSON…"-query of the Wiktionary parser mentioned above …
But how?
I tried to work with
JSON.stringify(response.data)
and
JSON.parse(response.data)
What am I doing wrong? Any proposals?
Thank you in advance, Frank
The complete code of the query is
function getENWiktionaryInfo(word, wordLanguage, callback) {
// getJSON("https://en.wiktionary.org/w/api.php?format=json&action=query&titles={word}&rvprop=content&prop=revisions&redirects=1&callback=?".replace("{word}", word), function (data) {
// $.getJSON("https://en.wiktionary.org/wiki/abdico#Latin", function (data) {
let url = "https://en.wiktionary.org/w/api.php?format=json&action=query&titles={word}&rvprop=content&prop=revisions&redirects=1&callback=?".replace("{word}", word);
console.log("getENWiktionaryInfo " + url);
axios({
method: 'get',
url: url,
}).then((response) => {
var results = {
title: "",
definitions: [],
examples: []
}
let data = response.data;
console.log("DATA "+data);
const jsonObj= JSON.stringify(response.data)
//let data = jsonObj;
var title, content;
if (!data || !data.query || !data.query.pages || data.query.pages[-1]) {
return callback({});
}
callback(results);
});
}
The pure call for the (latin) word "res" is:
https://en.wiktionary.org/w/api.php?format=json&action=query&titles=res&rvprop=content&prop=revisions&redirects=1&callback=?
2
Answers
I hope I have not bothered you with my problem ... had a conversation here, but all the comments disappeared?
Anyway ... there must be a difference between Axios and other queries like fetch(...) or getJSON(...) - see discussion here and here
I tried to reenact this with the console at Google chrome ... but I was not able to use axios there ... I tried to set up Codesandbox.io for this, but then
My last idea was: replacing this query
by this
... and it worked ... that was astonishing.
This different query seems to return the raw markup text (unfortunately, I'm not able to remember where I found it), but that's exactly what I need. For the future, it will be my task to learn more of the javascript details of axios and so on ...
I would like to parse this text and extract the information I need - using this wiktionary parser mentioned above as a template.
Many thanks to all who tried to help here!
Kind regards, Frank
You can use it like this which would be better