Reputation: 47
I am creating a javascript function to use in glideapps, following this process. The function takes in a google maps api url as a string input, and returns the json file as a string output. Using the example from google, I created this test which works fine.
function test(request_url){
var axios = require('axios');
var config = {
method: 'get',
url: request_url,
headers: { }
};
var final = axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
})
return final}
When I use the same function in the code for the glideapp function, I get two errors. First, I got the error: Error: Module name "axios" has not been loaded yet for context: _. Use require([])
and addressed it by using var axios = require(['foo'], function (foo)
instead.
window.function=function(request_url){
var axios = require(['axios'], function (axios) {
//axios is now loaded.
});
var config = {
method: 'get',
url: request_url,
headers: { }
};
var final = axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
})
return final}
Now I get the error TypeError: axios(...).then is not a function
. How do I fix this? Thank you
Upvotes: 1
Views: 3437
Reputation: 1
Be careful when importingaxsios
{}
Must be entered without {Axios} ===false
Axios === true
Upvotes: -1
Reputation: 534
Use the axios variable inside the function of require instead of the returning value of require function.
require(['axios'], function (axios) {
window.function=function(request_url){
var config = {
method: 'get',
url: request_url,
headers: { }
};
var final = axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
})
return final
}
});
Upvotes: 1