Get a json file's data without using require with a variable as path - react-native

I'm trying to get some data from a JSON file, but I can't use require because I need the path to be a variable and if I try to use require with a variable I get an error:
invalid call
Here is the function :
async fetchData(dataPath){
require(dataPath);
return data;
}
The dataPath variable is dependent on a selected button.

Hey since its static data could you simply make an object of all possibilities
const allPossibleData = {
customData1: require('./pathToData1'),
customData2: require('./pathToData2')
}
and simply get data by allPossibleData['customData1'], since dynamic require with a variable is not possible otherwise, you can simply do it like this
fetchData(require('dataPath'))
and on function instead getting dataPath as rgument simply get data.

Related

var error = new Error(message); Why am I getting this message in the terminal?

Everything works effectively until I add the const amountOut to the file.
Code with Error & location of function that breaks code
You are passing 2 parameters into a function that is expecting 3. The second parameter is one value because you have it as an array in brackets - [AddressFrom, AddressTo].
You will need to either modify how you are passing in the parameters to the getAmountsOut function or modify the getAmountsOut function to accept 2 parameters, with one being an array.

How to use split function on object data in react

I am getting data in below eg format and I want to use split function on 'testabc1223./67767' but I can't use it as it is an object and split cannot be used on object please let me know how can I use split on this
{
name: 'testabc1223./67767'
}
You shuld get the object property like that:
YOUR_OBJECT.name.split(...);

How to set a global constant from a function Obj C

I need to set a global const at runtime. As far as I understand they are set at compile time, however I'm using a global const as a url string thats referenced throughout the app. Depending on a option selected before the user logs in, I need the url string to change. This will only happen at run time before a user has logged in (for testing purposes)
I know an alternative is to just use a global variable (not constant) but I feel like thats not the best practice.
Any help would be much appreciated
You can save the url in NSUserDefaults Class of Objective-C and can change the url once user logged in.
A constant in Objective-C has a very specific meaning - it's something known at compile time. If you need to set a value at runtime, you need a read-only property or a function returning the value.
If you decide to take a read-only property route, make a global singleton object, and put the desired property on it. Give the property read-only access, and use it throughout your program.
If you decide to use a global function, you can do it like this:
// This goes in the header
extern const char *urlString();
// This goes into the implementation file
static char *urlStringVal = NULL;
const char *urlString() {
return urlStringVal;
}
Any function in the same implementation file as urlString has writing access to urlStringVal, and can change it as needed.

Manipulate Result type with JSON as content

This is a follow-up on the question Why do I get a list of numbers instead of JSON when using the Twitch API via Rust? If I use the solution suggested in the previous post:
Use the response.get_body() method to get a list of byte number which can be converted to a Result with from_utf8() method.
This returns a Result with everything in it. I'm not sure how to manipulate it. I was hoping I could use it like an array but the docs and rustbyexample don't seem to explain it. What is the purpose of a Result type?
This is the exact response that I'm getting from the body after converting it to UTF-8.
The Result type does not help you here – it just stores arbitrary data and is used for error handling (instead of exceptions). But you can use the rustc_serialize crate to parse the string returned by the Result:
extern crate rustc_serialize;
use rustc_serialize::json::Json;
fn main() {
let response_result = /* ... */;
let data = response_result.unwrap();
let json = Json::from_str(&data).unwrap();
println!("{}", json.find("status").unwrap());
}

Lua - How do I dynamically call a module?

Here's some much-simiplified Lua code I'm working with. I need to know how to dynamically call another module ('zebra'):
avar = require "avar"
bvar = require "bvar"
function create(zebra)
print(zebra.new())
end
print(create(avar))
And here are two modules:
local Avar = {}
function Avar.new()
return "avar"
end
return Avar
local Bvar = {}
function Bvar.new()
return "new"
end
function Bvar.old()
return "old"
end
return Bvar
If I try to pass in the string "avar" to my 'create' function, it doesn't work. If I pass in the word 'avar' with no quotes, it does work, however, I don't understand what avar with no quotes is? It seems to be a blank table? Not sure how to pass a blank table as an argument in my main program.
But maybe I'm totally on the wrong path. How do I dynamically call modules?
You can require any time:
function create(zebraModuleName)
zebraType = require(zebraModuleName)
print(zebraType .new())
end
print(create("avar"))
print(create("bvar"))
avar without the quotes is a global variable you created. It is initialized to the value returned by the require function1, which is the value returned by the module you are invoking. In this case, its a table with the new field that happens to be a function.
1 Importing a modules in Lua is done via regular functions instead of a special syntax. The function call parenthesis can be ommited because parens are optional if you write a function call with a single argument and that argument is a string or a table.
Other than that, there are also some other things you are confusing here:
The table you are storing on avar is not empty! You can print its contents by doing for k,v in pairs(avar) do print(k,v) end to see that.
The avar, bvar and create variables are global by default and will be seen by other modules. Most of the time you would rather make them local instead.
local avar = -- ...
local bvar = -- ...
local function create (zebra)
-- ...
end
The create function clearly expects a table since it does table indexing on its argument (getting the new key and calling it). The string doesn't have a "new" key so it won't work.
You aren't really dynamically calling a module. You are requiring that module in a regular way and it just happens that you pass the module return value into a function.
create always returns nil so there is no point in doing print(create(avar)). You probablu want to modify create to return its object instead of printing it.
You can use standard require from lua language or build your own loader using metatables/metamethods.
1. create a global function:
function dynrequire (module)
return setmetatable ({},{
__index = function (table,key)
return require(module..'.'..key)
end
})
end
2. Create your project tree visible to package.path
./MySwiss/
\___ init.lua
\___ cut.lua
\___ glue.lua
\___ dosomething.lua
3. Make your module dynamic
you only need to put this line on your MySwiss/init.lua (as if you were namespacing a PHP class):
return dynrequire('MySwiss')
4. Require your module and use subproperties dynamically
On your script you only need to require MySwiss, and the folder file (or subfolders with dynrequire('MySwiss.SubFolderName').
var X = require('MySwiss')
X.glue()
Note that MySwiss doesn't have glue key. But when you try access de glue key the metamethod __index try to require submodule. You can the full project tree using this technique. The only downside is the external dependencies not packed this way.