Field code names are automatically generated for fields when they are first dragged into the form of the Kintone App settings. The field codes can be update through the App's settings. Follow
the guide in the Help documents
to update the Field codes.
Through the App's settings, let's set:
The "Title" field to have the field code
title
The "Author" field to have the field code
author
The "Record number" field to have the field code
recordID
(you'll need to drag and drop the Record Number field onto the form to update it's field code)
The
app
parameter is the same as before, pointing to the App ID of the Kintone App we want to get data from. The
query
parameter,
query=order by recordID asc
states to retrieve records from the Kintone App in ascending order of the Record Number value. You can check the
Kintone API documents
for more details on queries.
Let's attach
parameters
to the end of the API request endpoint in the fetch request.
app.get('/getData',cors(corsOptions),async(req,res)=>{constfetchOptions={method:'GET',headers:{'X-Cybozu-API-Token':process.env.API_TOKENconstparameters="?app=1&query=order by recordID asc";constresponse=awaitfetch(requestEndpoint+parameters,fetchOptions);constjsonResponse=awaitresponse.json();res.json(jsonResponse);Enter fullscreen modeExit fullscreen mode
Since we're collecting a list of records from our Kintone App, let's render the response as a list. Note that Kintone's Get Records API responds the list of records as an array of objects. We'll follow the React document's example on how to handle our array to be rendered as a list in our React App.
The first thing we'll do is update the callRestApi function. Let's remove the stringify statement, since in the code the response is easier to handle as JSON format.
constarrayOfLists=jsonResponse.records.map(record=><li><b>{record.title.value}</b> written by {record.author.value}</li>returnarrayOfLists;Enter fullscreen modeExit fullscreen mode
Note here that we are referencing our field values in our records by stating record.title.value (the value of the Title field) and record.author.value (the value of the author field).
The resulting array of lists will be returned to the useEffect hook, which will update the apiResponse state using setApiResponse (we don't need to make any changes to the code here).
In the return statement of the RenderResult function, let's place {apiResponse} between <ul> elements, so we can render the array of lists.
Aha...there seems to be a warning about "keys" 🗝️🤔
The react.js documents states the following about keys:
Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity
Basically, we need to assign a unique ID to each list we create. Instead of manually creating these keys though, we'll use the unique IDs stored in our Kintone App. The Record number field in Kintone is an auto-incremented field, unique to each record. In our map function, let's add the key attribute to our li element, and assign the value of the Record number field by stating key={record.recordID.value}.
//return JSON.stringify(jsonResponse);constarrayOfLists=jsonResponse.records.map(record=><likey={record.recordID.value}><b>{record.title.value}</b> written by {record.author.value}</li>returnarrayOfLists;Enter fullscreen modeExit fullscreen mode
require('dotenv').config();constexpress=require('express');constcors=require('cors');constfetch=require('node-fetch');constPORT=5000;constapp=express();app.use(cors());constcorsOptions={origin:"http://localhost:3000"constrequestEndpoint="https://{subdomain}.kintone.com/k/v1/records.json";app.get('/getData',cors(corsOptions),async(req,res)=>{constfetchOptions={method:'GET',headers:{'X-Cybozu-API-Token':process.env.API_TOKENconstparameters="?app=1&query=order by recordID asc";constresponse=awaitfetch(requestEndpoint+parameters,fetchOptions);constjsonResponse=awaitresponse.json();res.json(jsonResponse);app.listen(PORT,()=>{console.log(`Example app listening at http://localhost:${PORT}`);Enter fullscreen modeExit fullscreen mode
importReact,{useState,useEffect}from'react';importReactDOMfrom'react-dom';constrestEndpoint="http://localhost:5000/getData";constcallRestApi=async()=>{constresponse=awaitfetch(restEndpoint);constjsonResponse=awaitresponse.json();console.log(jsonResponse);//return JSON.stringify(jsonResponse);constarrayOfLists=jsonResponse.records.map(record=><likey={record.recordID.value}><b>{record.title.value}</b> written by {record.author.value}</li>returnarrayOfLists;functionRenderResult(){const[apiResponse,setApiResponse]=useState("*** now loading ***");useEffect(()=>{callRestApi().then(result=>setApiResponse(result));},[]);return(<h1>ReactApp</h1>
<ul>{apiResponse}</ul>
ReactDOM.render(<RenderResult/>,document.querySelector('#root')Enter fullscreen modeExit fullscreen mode
Built on Forem — the open source software that powers DEV and other inclusive communities.