简体   繁体   English

如何将新的 object 发布到现有的 json(客户端 ReactJS + 服务器端 NodeJS 和 Express)

[英]How to post new object to the existing json (clientside ReactJS + server side NodeJS and Express)

Pretty new with NodeJS and Express but want to figure out it is possible to add new objects after loading api via client side. NodeJS 和 Express 相当新,但想弄清楚在通过客户端加载 api 后可以添加新对象。 So in the first place it should load the api via server-side which contains the existing objects and output on the client side, and when that is loaded I want to add new objects that I post via server side.因此,首先它应该通过服务器端加载 api,其中包含现有对象和客户端的 output,当加载时我想添加我通过服务器端发布的新对象。 So the new posted object should be added added to the json.所以新发布的 object 应该添加到 json 中。

Client Side客户端

 import { useEffect, useState } from 'react'; import axios from 'axios'; export const App = () => { const [inputValue, setInputValue] = useState(""); const [posts, setPosts] = useState([]); useEffect(() => { const headers = { "Content-Type": "application/json" }; try { const getData = async () => { const result = await axios.get('http://localhost:5000', headers); console.log(result.data); }; getData(); } catch (error) { console.log(error); } }, []); const postReview = async value => { const headers = { "Content-Type": "application/json" }; let data = { name: value }; try { const getData = async () => { const result = await axios.post('http://localhost:5000/songs', data, headers); console.log(result.data); }; getData(); } catch (error) { console.log(error); } }; const submitReview = () => { postReview(inputValue); }; return ( <div className="App"> <input placeholder="Place review" onChange={e => setInputValue(e.target.value)} /> <button onClick={() => submitReview()}>Send</button> </div> ); }; export default App;

Server Side服务器端

 const express = require('express'); const cors = require('cors'); const axios = require('axios'); const app = express(); const port = 5000; app.use(cors({ origin: '*' })); app.use(express.json()); app.get("/", async (req, res, next) => { try { const { data } = await axios.get('https://raw.githubusercontent.com/XiteTV/frontend-coding-exercise/main/data/dataset.json'); res.status(200).send(data.videos); } catch (ex) { res.status(500).send(data.videos); } }); app.post('/songs', async (req, res) => { console.log(reviews.length); });

I didn't understand what you meant, but I think you want to add more objects to the list of posts, if so, use the following:我不明白你的意思,但我认为你想在帖子列表中添加更多对象,如果是这样,请使用以下内容:

setPosts([
   ...posts,
   {
      //New post object
   }
])

You can post directly in their API.您可以直接在他们的 API 中发布。

 const postReview = async review => { const headers = { "content-type": "application/json", "Access-Control-Allow-Origin": "*" }; // axios post request to that api const response = await axios.post('https://jsonplaceholder.typicode.com/posts', {review}, {headers}); };

They described this on their site.他们在他们的网站上描述了这一点。 Further read进一步阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 nodejs express 4.0发布json对象 - nodejs express 4.0 post json object 将JSON对象从客户端JavaScript传递到NodeJS - Passing a JSON object from clientside JavaScript to NodeJS 如何将新的object更新为ReactJS中已有的object? - How to update new object into the existing object in ReactJS? 如何将JS对象从服务器端传递到NodeJS中的客户端 - How can I pass JS objects from the server-side to the clientside in NodeJS 如何处理nodejs上的window对象以进行reactjs应用程序的服务器端呈现 - How to handle window object on nodejs for server-side rendering of reactjs application 如何将数据从ReactJS发布到NodeJS Express API - How to post data from ReactJS to NodeJS Express API 如何在 NodeJS 中的现有 object 中添加新数组 - How to add new array in existing object in NodeJS 如何使用 express Mongoose 在服务器端 Nodejs 上优化 api 查询 - How to optomized the api query on server side Nodejs with express Mongoose 在发送到服务器之前,如何生成POST数组作为客户端/ JavaScript对象? - How can I generate the POST array as a clientside / JavaScript object before sending to server? 如何从服务器(Node JS和Express)的客户端上接收和使用JSON对象? - How do I receive and use a JSON object on the client-side from the server (Node JS and Express)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM