I have an API utils file React JS where i have list of endpoints declared like below-
File1.js-
export const api_endpoints = {
api1: ()=> '../data/user'
}
I want api1() from File1.js in my another file (File2.js) hence i am importing this like below-
import {api_endpoints} from '../File1';
const myObj = {
[api_endpoints.api1()]: '../data/user2'
}
This gives me error in File2 saying-
"cannot read property api1 of undefined"
I am not sure why api_endpoints comes as undefined in File2.js. The paths are correct. Can someone help me understand that.
Looks like you are importing from wrong path. Tried the above example and it is working fine for me..
https://codesandbox.io/s/objective-meninsky-7gbch?file=/src/File2.js
You might have just copied it wrong, but you imported {api1_endpoints}, but are asking for api_endpoints
Related
Please don't judge, i've no idea what to do and how to do :)
My code:
ether.js ( ^5.6.0)
import { ThirdwebSDK } from '#3rdweb/sdk'
import { ethers } from 'ethers'
const sdk = new ThirdwebSDK(
new ethers.Wallet(
process.env.METAMASK_PRIVATE_KEY,
ethers.getDefaultProvider(
'https://rinkeby.infura.io/v3/'
)
)
)
bug
If you are using JavaScript and getting this error then You made a mistake while importing dotenv. you are using ".config" as property . Use it like a method like following
require("dotenv").config()
It will work.
Another problem you might have is where your .env is located. Make sure that your .env is in the same folder as your code.
You most likely messed up env variable. Check if METAMASK_PRIVATE_KEY is the correct name. Because otherwise it will throw undefined and one of the ethersjs library will try to run hexToString() method on undefined value, thus you get the error.
EDIT:
You might as well forget to include this in your code:
import {} from 'dotenv/config'
// or if its not ESmodule
require('dotenv').config()
Absence of this import will throw undefined as well when you access env variable.
I had this problem aswell. I used a constructor for my sol contract that looked as following:
constructor(
address vrfCoordinatorV2,
uint256 entranceFee,
bytes32 gasLane,
uint64 subscriptionId,
uint32 callbackGasLimit,
uint256 interval
)
After alot of frustration i found out that my arguments for the deploy script wasn't lined up in the same order. This was causing the error:
"Cannot read properties of undefined (reading 'toHexString')"
In my case the arguments needed to be lined up as following:
const args = [
vrfCoordinatorV2Address,
networkConfig[chainId]["raffleEntranceFee"],
networkConfig[chainId]["gasLane"],
subscriptionId,
networkConfig[chainId]["callbackGasLimit"],
networkConfig[chainId]["keepersUpdateInterval"],
];
Another option is that you've edited the .env file and forgotten to save it. Hence the script won't see the new entry.
I had a similar issue, I actually missed saving an updated .env file. When I saved the updated .env properly this worked.
I have a typescript file in which there are some functions for validating objects of different classes. So, I have to import those classes into the ts file. For instance,
import { UserDetail } from "../../entity/user/userDetail";
The following block of code depicts a function which is used for validating the objects of the mentioned class.
export function validateSignUpData(userDetail:UserDetail):Array<String>{
let errorMessages = new Array<String>();
/* Some Code */
As you can see the imported class has been used as the function's input type. Now the problem is that it seems we are not allowed to use both import and module.exports simultaneously! Therefore, I am not able to use require in order to get the mentioned function in the Node server file.
When I add the following block of code to my ts file, the following exception is thrown!
module.exports = validateSignUpData;
TypeError: Cannot assign to read only property 'exports' of object '# object'
Question: How can I use all import, modules.export, and export in the same ts file?
I will be glad if someone can help me with the situation.
Thank You
Of course you're able to import that function with require. Just do it like this:
const {validateSignUpData} = require('./my-module.js')
And pay attention to require the compiled javascript file, not the typescript file. They may be in different folders, depending on your build setup.
I'm trying to import a local file to use the data in it.
I have tried the require method and the import method. Neither seems to work.
I'm using Angular 5 and Typescript 2.9.2. I am locked to angular 5.
I'm building for aot and when I look at the fesm5 file that gets exported the json file does not seem to resolve.
(I DO NOT WANT TO PULL IT IN WITH HTTP...)
import * as data from '../../assets/file.json';
When I try to set tsconfig value to:
"resolveJsonModule": true,
I get an error on build:
Cannot read property 'slice' of undefined
TypeError: Cannot read property 'slice' of undefined
at addReferencesToSourceFile (/Users/ME/Projects/PROJECT/node_modules/#angular/compiler-cli/src/transformers/compiler_host.js:520:54)
at TsCompilerAotCompilerTypeCheckHostAdapter.getSourceFile (/Users/ME/Projects/PROJECT/node_modules/#angular/compiler-cli/src/transformers/compiler_host.js:348:13)
I have also tried the:
declare module '*.json' {
const value: any;
export default value;
}
This doesn't seem to help at all.
Also tried:
declare function require(url: string);
var data = require('../../assets/file.json');
This does not seem to resolve the json in the final file either.
You can put your file under assets folder and get it using HTTP GET from HttpClient module, this will return the whole json file, use your file path in your GET request URL; something like this:
this.http.get('../assets/file.json')
.subscribe(jsonFormat => {
//Your code
});
You can create a service for all the GET, POST, PUT& DELETE implementations. Then in your GET method of HttpClient that is imported from ‘#angular/common/http’, call the JSON with the link to your local JSON file.
public getAbout(): Observable<any> {
return this.http.get("./assets/json/about.json");
}
I have the following problem, I get:
"Unable to resolve module nik/env from /some/path/constant.js : Module does not exist in the module map or in these directories /path/to/project/root"
In my constant.js I do try to do the following:
import {
API_URL,
} from 'nik/env'
so I am basically just trying to get a variable (API_URL) out of my env.
However, when I tested this and I replaced the above with a hardcoded variable like so: const API_URL = 'http://my-api.myserver.com';, I still get the same error. Namely, that it cannot resolve nik/env - but there is no reference to nik/env in that file anymore.
Why could this be? Thanks a lot in advance!!
I am trying to create a highstock. But I am getting require is not a function in console. I have tried several ways. Able to remove error from file but still getting error in console.
https://stackblitz.com/edit/highchart-highstock
Help will be highly appreciated
First, please use import syntax, instead of require and import Highstock module before the highchartsFactory() function definition, then you will be able to return the imported module from your factory function. Please take a look at code below:
import stock from 'highcharts/highstock';
export function highchartsFactory() {
return stock
}
Live example: https://stackblitz.com/edit/highchart-highstock-zjv1n3
Here is the answer for your question:
https://forum.highcharts.com/highcharts-usage/require-is-not-a-function-in-angular2-highchart-t40602/