Axios Problems (uncaught in promise) using zustand - node.js

Hi there i have problems with making the api call to my server. Using Postman i receive correctly my data from the URL but when i come into react and i use Axios to get those data i have the Axios error Uncaught in promise status code: 300 Here my code.
import {create} from 'zustand'
import axios from 'axios'
const URL1 = "http://localhost:5000/livespa"
export const useStore = create((set, get) => ({
liveSpa:[],
loadLivespa: async () => {
const response = await axios.get(URL1)
set({ liveSpa: await response.data})
},
}))
And my frontend page is like
const LiveSpa = () => {
const data = useStore((state) => state.liveSpa)
const loadBets = useStore((state)=> state.loadLivespa)
useEffect(() => {
loadBets()
}, [])
return (...)
}
I tried using Fetch but same problems, debugging my node server but it works fine using Postman

Related

mocking a api call not returning data

Trying to mock the following api to return data.
I keep getting the error:
ReferenceError: Cannot access 'mockedApi' before initialization
Mock Code
const mockedApi = jest.fn();
jest.mock("../../../utils/api", () => ({
...jest.requireActual("../../../utils/api"),
get: jest.fn(),
}));
When I wrap it in a function then the response doesn't work.
const mockedApi = jest.fn();
jest.mock("../../../utils/api", () => ({
...jest.requireActual("../../../utils/api"),
get: () => mockedApi,
}));
when I do a log on the api its showing get as a function now that doesn't return anything. when is should be returning data if I was to use. ?
mockedApi.mockResolvedValueOnce({ data: 'hello });
Do I even need to use ...jest.requireActual("../../../utils/api")
I thought this would insure the original methods would not get mocked and only the once I add would be mocked like get. But this doesn't seem to be the case the entire file and all its methods get mocked ?
File been mocked
import axios from "axios";
const api = axios.create({
baseURL: process.env.REACT_APP_SPARQL_URL,
responseType: "json",
});
export const encode = (arr) => {
const urlencoded = new URLSearchParams();
arr.forEach((item) => {
urlencoded.append(...Object.keys(item), ...Object.values(item));
});
return urlencoded;
};
export default api;

Axios request returning an error in React js

I want to display the list of products in a webPage but I am getting an error.
I am using the async/await method on the axios.
The code:
const [products, setProducts] = useState([]);
useEffect(() => {
const fetchProducts = async () => {
const { data } = await axios.get("/api/products");
setProducts(data);
};
fetchProducts();
}, []);
I have installed cors and set the proxy as well in the front-end side package.json file but still getting an error like following:

How to scrape a website from opensea using node js

I'm failing at trying to scrape a collection of polygon NFTs from Opensea. Could someone please provide an example that prints the html returned in console? I tried the code below:
const https = require("https");
https.get("https://opensea.io/collection/orathai", response => {
console.log(response)
});
const axios = require('axios').default;
axios.get('https://api.opensea.io/api/v1/collection/orathai/?format=json')
.then(resp => {
console.log(resp.data);
})
.catch(err => {
// Handle Error Here
console.error(err);
});
don't forget : npm i axios
Refrence :
opensea api docs
Axios

Get external api with axios and req body to url for search and ajax NodeJS express

I use node.js expess with MVC pattern and use axios for get json url. The all logic request api i wrote it in Controller , I try to create some get api with axios like this.
this my chartController.js , it's some logic for get json from url with req.body.symbol (it's just symbol of stock from user submitted input form , I want to get that symbol to my axios get url in code below in const chartChartPage )
'use strict';
const axios = require('axios');
const request = require('request');
/* For Get my Api Token Data */
const dataTokens = require('../config/apitokens');
const chartChartPage = async (req,res) => {
try {
const symbol = req.body.symbol;
const url = `https://${dataTokens.sandbox}.iexapis.com/stable/stock/${symbol}/chart/1m?token=${dataTokens.tokens}`;
const fetchData = await axios.get(url);
res.status(200).json(fetchData.data);
}
catch (error){
res.status(400).send(error.message);
}
}
module.exports = {
chartPage,
chartChartPage
}
Now, i have to added some routes in my chart.js
i think i should add router.post('/', chartChartPage) for get that symbol in input when user submitted Maybe i'm wrong.
var express = require('express');
var router = express.Router();
var {chartPage , chartChartPage} = require('../controllers/chartControllers');
router.get('/', chartPage);
router.post('/', chartChartPage);
module.exports = router;
and in my chartChartPage.js (it's just js file for my template)
and use ajax to get that data from url above (with chartChartPage) to get data for build chart stock
and try to console.log that data but that's not work in console
$(function(){
chartChartPage();
});
const chartChartPage = async () => {
await $.ajax({
url: '/chart',
type: 'POST',
dataType: 'json',
success: (response) => {
if (response != null) {
console.log(response);
}
},
error: (err) => {
console.log(err);
}
});
}
and when i submitted form i got that all json data in my page, but i want when submitted it's render to same page and work in console.log in ajax get url above. How can i fix.?
enter image description here

429 Error Code from axios api GET request

I'm having an issue with a 429 code error calling for too many requests that I am completely unsure how to solve. I'm getting the error when trying to make an axios api call to my Express server from my React frontend app.
Here is the backend route:
app.get('/api/fetchproducts', async (req, res) => {
const products = await Product.find({});
res.send(products);
});
And here is the axios call from the frontend:
import axios from 'axios';
import { FETCH_PRODUCTS } from "./types";
export const fetchProducts = () => async dispatch => {
const res = await axios.get('/api/fetchproducts');
dispatch({ type: FETCH_PRODUCTS, payload: res.data });
};
The error in the console is:
Uncaught (in promise) Error: Request failed with status code 429
Can anyone help me with this issue?
Please let me know if further information is required
this kind of an error normally occurs when your API is having rate-limiting enabled.

Resources