I am working on ReactJs in which i am trying to use third party api. As i tested the api in postman, all the api's are working fine. But when i integrated the api in my project i am getting CORS error.
I have searched on the google. I found the solution that use Proxy server. I tried using proxy server but still not working for me. May be I did not implement it correctly.
I referred these links :
https://daveceddia.com/access-control-allow-origin-cors-errors-in-react-express/
https://levelup.gitconnected.com/overview-of-proxy-server-and-how-we-use-them-in-react-bf67c062b929
DO i need to install anything?
Any help is appreciated.
package.json
{
"name": "WebPortal",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --port 8081 --hot --host localhost",
"build": "webpack --mode production"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"#material-ui/core": "^4.9.0",
"#mdi/font": "^4.9.95",
"babel-loader": "^7.1.5",
"bootstrap": "^4.4.1",
"bootstrap4-toggle": "^3.6.1",
"jquery": "^3.4.1",
"pm2": "^4.2.3",
"popper.js": "^1.16.1",
"react": "^16.12.0",
"react-bootstrap-toggle": "^2.3.2",
"react-datetime-picker": "^2.9.0",
"react-dom": "^16.12.0",
"react-easy-crop": "^2.0.1",
"react-image-crop": "^8.5.0",
"react-loader-spinner": "^3.1.5",
"react-router-dom": "^5.1.2",
"react-toasts": "^3.0.6",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.10.1"
},
"devDependencies": {
"#babel/core": "^7.4.3",
"#babel/plugin-proposal-class-properties": "^7.8.3",
"#babel/preset-env": "^7.4.3",
"#babel/preset-react": "^7.0.0",
"babel-core": "^7.0.0-bridge.0",
"babel-loader": "^8.0.5",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"css-loader": "^3.4.2",
"file-loader": "^5.0.2",
"html-webpack-plugin": "^3.2.0",
"style-loader": "^1.1.3",
"webpack": "^4.30.0"
},
"proxy": "api.sysco.com"
}
temp.js
fetch('/token?grant_type=client_credentials', {method: 'POST'}).then(
data => {
console.log("data==========>", data);
},
error => {
console.log("error==========>", error);
}
);
If you want a way to do this for development purposes (as some apis don't allow adding localhost or http to allow origin) you can use cors disabled mode of the browsers like for chrome
google-chrome --disable-web-security does the trick OR in windows you can create a shortcut (rightclick > send to desktop ) and in its properties(right-click > properties) add the flag --disable-web-security after ***/chrome.exe (you will see a textbox).
If you want a solution for production, there is no other way than configuring the api for your website. You can follow the link provided by Dominic to learn more about cors. All the third pary API uses allow any origin header or atleast give a way to configure cors in console.
Related
I am building an app using React / Redux and Firestore. I have done this before with Angular and React. The permissions on my firestore database are wide open for development purposes.
When I am not logged in, everything works well. However, when I log in I get an error the I have Missing or insufficient permissions.
Here is my rules file:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
Here is my package.json file.
redux-firestore listener error: FirebaseError: Missing or insufficient permissions.
at new n (webpack-internal:///336:485:23)
at eval (webpack-internal:///336:9625:28)
at eval (webpack-internal:///336:9626:18)
at n.onMessage (webpack-internal:///336:9659:10)
at eval (webpack-internal:///336:9565:26)
at eval (webpack-internal:///336:9596:37)
at eval (webpack-internal:///336:5192:31)
I had a fair bit of difficulty with versions and getting everything to work. Is it possible that this is a similar conflict?
{
"name": "triviaapp",
"version": "1.0.0",
"main": "index.js",
"author": "Joshua Foxworth",
"license": "MIT",
"scripts": {
"serve": "live-server public/",
"build": "webpack",
"dev-server": "webpack-dev-server"
},
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^1.2.32",
"#fortawesome/free-solid-svg-icons": "^5.15.1",
"#fortawesome/react-fontawesome": "^0.1.12",
"babel-cli": "6.24.1",
"babel-core": "6.25.0",
"babel-plugin-transform-class-properties": "6.24.1",
"babel-plugin-transform-object-rest-spread": "6.23.0",
"babel-preset-env": "1.5.2",
"babel-preset-react": "6.24.1",
"bootstrap": "^4.5.3",
"css-loader": "0.28.4",
"firebase": "^8.0.2",
"firebase-tools": "^8.15.0",
"live-server": "^1.2.0",
"node-sass": "^5.0.0",
"normalize.css": "7.0.0",
"react": "16.14.0",
"react-bootstrap": "^1.4.0",
"react-dom": "16.14.0",
"react-modal": "2.2.2",
"react-redux": "5.0.5",
"react-redux-firebase": "2.2.4",
"react-router-dom": "4.1.2",
"redux": "3.7.2",
"redux-firestore": "^0.14.0",
"redux-thunk": "^2.3.0",
"sass-loader": "6.0.6",
"style-loader": "0.18.2",
"uuid": "3.1.0",
"validator": "8.0.0",
"webpack-dev-server": "2.5.1"
},
"devDependencies": {
"#babel/core": "^7.12.3",
"#babel/preset-env": "^7.12.1",
"babel-loader": "^7.1.5",
"webpack": "^3.12.0"
}
}
Here is the action where I add a new "question". This works if I am not logged in, but not if I am logged in.
// These are action creators. This is possible because we are using the middleware thunk
export const createQuestion = (question) => {
console.log(question);
return (dispatch, getState, {getFirebase, getFirestore}) => {
// make async call
const firestore = getFirestore();
console.log(firestore);
firestore.collection('questions').add({
...question,
userId : 'tbd',
userName : 'tbd',
dateCreated:new Date()
}).then(()=>{
dispatch({type:'CREATE_QUESTION', question});
}).catch((err)=>{
dispatch({type:'CREATE_QUESTION_ERROR', err});
})
}
}
I am posting the "solution" here so that it anyone comes across this problem they don't waste as much time as I have. I manually created a user to work with in the firebase system. For whatever reasons, that was the problem. Once I built out more of the app and worker with a user created through the app, it was fine.
There you go.
I'm trying to work with this twitch npm package (https://www.npmjs.com/package/twitch) and am running into some issues when deploying via creat-react-app / react-scripts.
From my understanding, the webpack config that is bundled with create-react-app doesn't like .mjs files that this npm package is using. So, I get the error below when I try to build the app.
./node_modules/twitch/es/API/Kraken/Channel/ChannelApi.mjs
app_1 | Can't import the named export 'Cacheable' from non EcmaScript module (only default export is available)
If I manually deleted the "es" folder, then the build worked and everything functioned as expected. However, this isn't a real solution because when I push to production and deploy there the node modules are re-installed and the build fails once again.
Build processes aren't really my strong suit and after googling around for a while I'm unable to find a solution. If anyone can assist or can point me in the right direction, that would be much appreciated!
If it helps, here is my package.json
{
"name": "ui",
"version": "1.0.0",
"license": "UNLICENCED",
"private": true,
"dependencies": {
"#babel/core": "^7.9.0",
"#babel/plugin-syntax-dynamic-import": "^7.2.0",
"#babel/register": "^7.0.0",
"axios": "^0.19.2",
"babel-plugin-dynamic-import-node": "^2.2.0",
"btoa": "^1.2.1",
"clipboard-copy": "^3.0.0",
"connected-react-router": "^6.8.0",
"dateformat": "^3.0.3",
"dotenv": "^8.0.0",
"draft-js": "^0.11.0",
"draft-js-export-html": "^1.4.1",
"express": "^4.16.4",
"file-loader": "^3.0.1",
"firebase": "^5.2.0",
"history": "^4.7.2",
"human-date": "^1.4.0",
"ignore-styles": "^5.0.1",
"immutability-helper": "^3.0.0",
"jwt-decode": "^2.2.0",
"lodash": "^4.17.11",
"normalizr": "^3.2.4",
"prop-types": "^15.6.1",
"qs": "^6.5.2",
"react": "^16.8.0",
"react-animations": "^1.0.0",
"react-dnd": "^7.4.5",
"react-dnd-html5-backend": "^7.4.4",
"react-dom": "^16.8.0",
"react-ga": "^2.5.3",
"react-gtm-module": "^2.0.10",
"react-helmet": "^5.2.0",
"react-image-crop": "^8.3.0",
"react-is": "^16.8.0",
"react-loadable": "^5.5.0",
"react-loading-skeleton": "^2.0.1",
"react-on-screen": "^2.1.1",
"react-pdf": "^4.0.5",
"react-pose": "^4.0.6",
"react-redux": "^6.0.1",
"react-router": "^5.1.2",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.0",
"react-stripe-elements": "^2.0.0",
"redux": "^4.0.0",
"redux-devtools-extension": "^2.13.2",
"redux-thunk": "^2.2.0",
"reselect": "^3.0.1",
"semantic-ui-calendar-react": "^0.15.3",
"semantic-ui-css": "^2.4.1",
"semantic-ui-react": "^0.87.1",
"styled-components": "^4.2.0",
"twitch": "^4.2.4",
"url-loader": "^1.1.2",
"validator": "^11.1.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"lint": "eslint src",
"server": "NODE_ENV=production node server/bootstrap.js"
},
"engines": {
"node": "^10.0.0",
"yarn": "^1.12.3"
},
"devDependencies": {
"#babel/plugin-proposal-class-properties": "^7.4.4",
"#babel/plugin-transform-runtime": "^7.4.4",
"eslint": "^6.8.0",
"eslint-config-airbnb": "^18.1.0",
"eslint-config-prettier": "^6.10.1",
"eslint-plugin-import": "^2.11.0",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-react": "^7.8.1",
"eslint-plugin-react-hooks": "^3.0.0",
"prettier": "^2.0.2"
},
"proxy": "http://api:8080",
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"jest": {
"moduleNameMapper": {
"\\.worker.js": "<rootDir>/__mocks__/workerMock.js"
}
}
}
A suggestion presented at this GitHub comment was to add react-app-rewired to your project and then use this config-overrides.js file:
module.exports = function override(config) {
config.module.rules.push({
test: /\.mjs$/,
include: /node_modules/,
type: "javascript/auto"
});
return config;
}
In my project I was already using react-app-rewired, so I just added the rule from that snippet. This workaround fixed the error for me.
In the specific case of the twitch library, the author has suggested trying the #next release, although I haven't personally verified that solution yet.
Upgrading to the latest react-scripts version of 5.0.1 in my package.json as of writing this, has solved the issue for me.
Upgrading tolatest react-scripts version of 5.0.1 in my package.json as of writing this
use the code
npm install --save --save-exact react-scripts#5.0.1
or
yarn add --exact react-scripts#5.0.1
I am trying to run my app on Android but I am getting this error I don't know why I am getting this, I don't know what is causing this error in my project
I have tried tried many ways to resolve this issue but Still wasn't able to resolve this issue.
Tried To register two Views with the same name RTCVideoView
My Package.json is
{
"name": "Hello World",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"dependencies": {
"#ptomasroos/react-native-multi-slider": "^1.0.0",
"#react-native-community/async-storage": "^1.6.2",
"#react-native-community/slider": "^2.0.0",
"axios": "^0.19.0",
"connectycube-reactnative": "^1.7.3",
"lodash": "^4.17.15",
"moment": "^2.24.0",
"prop-types": "^15.7.2",
"react": "16.8.6",
"react-native": "0.60.5",
"react-native-autogrow-textinput": "^5.2.0",
"react-native-cache-store": "^1.0.2",
"react-native-calendars": "^1.212.0",
"react-native-deck-swiper": "^1.6.7",
"react-native-image-picker": "^1.1.0",
"react-native-linear-gradient": "^2.5.6",
"react-native-modal-datetime-picker": "^7.5.0",
"react-native-navigation": "^3.0.0",
"react-native-scrollable-tab-view": "^0.10.0",
"react-native-swiper": "^1.5.14",
"react-native-vector-icons": "^6.6.0",
"react-native-view-overflow": "^0.0.4",
"react-native-webrtc": "^1.75.0",
"react-redux": "^7.1.1",
"redux": "^4.0.4",
"redux-logger": "^3.0.6",
"redux-persist": "^6.0.0",
"redux-thunk": "^2.3.0",
"rn-prompt": "^1.0.4"
},
"devDependencies": {
"#babel/core": "^7.6.0",
"#babel/runtime": "^7.6.0",
"#react-native-community/eslint-config": "^0.0.5",
"babel-jest": "^24.9.0",
"eslint": "^6.4.0",
"jest": "^24.9.0",
"metro-react-native-babel-preset": "^0.56.0",
"react-test-renderer": "16.8.6"
},
"jest": {
"preset": "react-native"
}
}
Any Help Would be Appreciated.
All you need is just clean up the cache by the following command
npm cache clean --force
This will resolve your problem most probably
The version of react-native-webrtc you have specified in your package.json is different from the version that connectycube-reactnative depends on. The latest connectycube-reactnative (1.8.0) depends on "react-native-webrtc": "1.69.1".
Double check the dependency of the version you have installed by looking at the node_modules/connectycube-reactnative/package.json and match the version specified in your package.json to the version the connectycube-reactnative requires.
The #arzundo answer is correct
And seems this issue is addressed in latest releases here https://github.com/ConnectyCube/connectycube-reactnative-samples/tree/master/RNVideoChat so you can simply try to pull the latest master
I am building a react app. I have used yeoman to generate the react app structure. I am using webpack to bundle all js files. My package.json is -
{
"dependencies": {
"axios": "^0.15.0",
"classnames": "^2.2.5",
"es6-shim": "^0.35.0",
"react": "^15.0.1",
"react-dom": "^15.0.1",
"react-mdl": "^1.7.2",
"react-redux": "^4.4.5",
"react-router": "^2.4.0",
"redux": "^3.5.1",
"todomvc-app-css": "^2.0.4"
},
"devDependencies": {
"autoprefixer": "^6.2.2",
"babel-core": "^6.13.0",
"babel-eslint": "^6.0.2",
"babel-loader": "^6.2.0",
"babel-plugin-istanbul": "^2.0.1",
"babel-polyfill": "^6.7.4",
"babel-preset-es2015": "^6.2.0",
"babel-preset-react": "^6.1.18",
"browser-sync": "^2.9.11",
"browser-sync-spa": "^1.0.3",
"css-loader": "^0.23.1",
"del": "^2.0.2",
"es6-shim": "^0.35.0",
"eslint": "^3.2.2",
"eslint-config-xo-react": "^0.7.0",
"eslint-config-xo-space": "^0.12.0",
"eslint-loader": "^1.3.0",
"eslint-plugin-babel": "^3.1.0",
"eslint-plugin-react": "^5.0.1",
"extract-text-webpack-plugin": "^2.0.0-beta.3",
"file-loader": "^0.9.0",
"gulp": "gulpjs/gulp#4ed9a4a3275559c73a396eff7e1fde3824951ebb",
"gulp-filter": "^4.0.0",
"gulp-hub": "frankwallis/gulp-hub#d461b9c700df9010d0a8694e4af1fb96d9f38bf4",
"gulp-sass": "^2.1.1",
"gulp-util": "^3.0.7",
"html-webpack-plugin": "^2.9.0",
"jasmine": "^2.4.1",
"json-loader": "^0.5.4",
"karma": "^1.3.0",
"karma-coverage": "^1.1.1",
"karma-jasmine": "^1.0.2",
"karma-junit-reporter": "^1.1.0",
"karma-phantomjs-launcher": "^1.0.0",
"karma-phantomjs-shim": "^1.1.2",
"karma-webpack": "^1.7.0",
"node-sass": "^3.4.2",
"phantomjs-prebuilt": "^2.1.6",
"postcss-loader": "^0.8.0",
"react-addons-test-utils": "^15.0.1",
"react-hot-loader": "^1.3.0",
"sass-loader": "^3.1.2",
"style-loader": "^0.13.0",
"webpack": "2.1.0-beta.20",
"webpack-dev-middleware": "^1.4.0",
"webpack-hot-middleware": "^2.6.0"
},
"scripts": {
"build": "gulp",
"serve": "gulp serve",
"serve:dist": "gulp serve:dist",
"test": "gulp test",
"test:auto": "gulp test:auto"
},
"eslintConfig": {
"root": true,
"env": {
"browser": true,
"jasmine": true
},
"extends": [
"xo-react/space",
"xo-space/esnext"
]
}
}
I am using axios to make http requests. Presently, I have not coded a server to return a json data. Hence, to test the app's functionality, I want to serve static json data from a json file.
How can I achieve this ?
Since this setup is running a web-server, I hope a json file can be served using something like - axios.get('http://localhost:3000/user.json').
If you're starting your app from the Facebook's 'create-react-app' instructions found here: https://facebook.github.io/react/docs/installation.html
Then I found an easy way to be able to access JSON files from the URL without having the React Router get in the way and intercept the request.
All you have to do is in your index.js of that 'create-react-app' scaffolding, add something like this as the very first line:
import './myJsonFile.json';
And that's it, as afterwards going to:
http://localhost:3000/myJsonFile.json
would correctly fetch the file without React router interfering in the lookup.
(maybe it wasn't exactly your question as you've used a different way to generate the app, but this would be an answer for those who use the 'create-react-app' as a base).
You can use the webpack file-loader to have webpack serving that file as if it was any other static files, e.g. an image.
You can try with this in your "main" (entrypoint) file:
require("file-loader?name=api/[name].[ext]!./user.json");
This require is just needed to tell webpack to "emit" that file in the output destination, otherwise it doesn't know anything about it.
You also need to install the file-loader:
npm install --save-dev file-loader
Then with webpack-dev-server you should be able to access that file from http://localhost:8080/api/user.json (replace localhost:8080 according to your webpack dev server config).
However, if you need to stub a json api, I'd rather use something like these:
https://www.npmjs.com/package/json-server
https://www.npmjs.com/package/dyson
that are more flexible and can handle more advanced thing like serving the response with the right content type, testing cross domain issues, JSONP, etc.
If you want something simpler, you can use this: https://www.npmjs.com/package/superstatic to simply serve static files,
or even simpler, check out how these one-liners to start a simple http server to serve your static json files:
https://gist.github.com/willurd/5720255
We are facing a problem with Relay/GraphQL/React in production. When testing in master everything works fine, however in production we get the following error in the console which causes everything related to Relay/GraphQL/React not to be working at all:
Uncaught Error: Relay transform error ``Map is not defined`` in file '/var/app/ondeck/app/react/relay/routes/xxx-route.jsx'. Try updating your GraphQL schema if an argument/field/type was recently added.
We believe is something related to the build of the application when it's getting deployed, however we have no idea where to start looking at.
This is my package.json in case it helps:
{
"private": true,
"devDependencies": {
"babel-eslint": "^6.0.4",
"eslint": "^2.11.1",
"eslint-plugin-react": "^5.1.1",
"webpack-notifier": "^1.3.1"
},
"dependencies": {
"autobind-decorator": "^1.3.3",
"babel-core": "^6.4.0",
"babel-loader": "^6.2.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-polyfill": "^6.7.4",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-stage-0": "^6.3.13",
"babel-relay-plugin": "^0.9.0",
"extract-text-webpack-plugin": "^1.0.1",
"graphiql": "^0.7.2",
"graphql": "^0.6.0",
"isomorphic-fetch": "^2.2.1",
"node-sass": "^3.3.3",
"postal": "^2.0.4",
"react": "^15.1.0",
"react-dom": "^15.1.0",
"react-linkify": "^0.1.1",
"react-notification-system": "^0.2.6",
"react-relay": "^0.9.0",
"react-star-rating-component": "^1.0.0",
"react-stars": "^2.1.0",
"react-webpack-rails": "^0.4.1",
"sass-loader": "^4.0.0",
"webpack": "^1.12.1"
},
"scripts": {
"build": "webpack -p --config webpack/production.config.js",
"start": "webpack -w --config webpack/dev.config.js",
"lint": "eslint --ext .js,.jsx app/react/**"
},
"license": "",
"engines": {
"node": ">= 0.10"
}
}
Just for the record, I needed to update Node.js to the latest version in order for everything to work again. Thanks.