라벨이 JSON인 게시물 표시

JSON 변환 응답 에러 - NestJS

이미지
 아래는 작성한 코드이다. for (let i = 0; i < reserve.length; i++) {       reserve[i]['requestProduct'] = [];       for (let j = 0; j < requestItems.length; j++) {         if (requestItems[j].reserveId === reserve[i].id) {           console.log("push requestProduct item : ", reserve[i]);           reserve[i]['requestProduct'].push(reserve[i]);         }       }     } [Nest] 713  - 07/25/2024, 9:37:19 AM   ERROR [ExceptionsHandler] Converting circular structure to JSON     --> starting at object with constructor 'BusinessRequestReserveEntity'     |     property 'requestProduct' -> object with constructor 'Array'     --- index 0 closes the circle 해당 오류( Converting circular structure to JSON )는 객체 내에 순환 참조(circular reference)가 있을 때 JSON으로 변환하려고 하면 발생합니다. NestJS에서 이런 문제가 발생하는 주된 이유는 ...

node.js Express 서버에 요청으로 들어온 Body를 JSON형태로 읽기

이미지
 안녕하세요. 알렉스 입니다. 이번에는 Express를 이용해서 Client에서 들어온 request의 Body를 JSON형식으로 읽어보도록 하겠습니다. const express = require ( 'express' ) const app = express (); // POST app . post ( '/login' , ( req , res ) => { console . log ( req . body ) // body확인 가능 부분 res . status ( 201 ); res . send ({ message : "Login Successful" , body : req . body }) }) app . listen ( 3500 , () => { console . log ( "Server is working" ); }) 위 코드와 사진을 확인하면 실제 Postman에서 request한 body내용이 전혀 읽혀지지 않은것이 확인이 됩니다. 이렇한 원인은 통신에 있습니다. http통신의 body는 한번에 들어오는 것이 아닙니다. 부분(또는 chunk) 부분 들어오고 그것을 조합하고 나야 완전한 body가 되는 것입니다. app . use ( function ( req , res , next ) { let data = '' ; // chunk를 하나로 모음 req . on ( 'data' , function ( chunk ) { data += chunk ; console . log ( chunk ); }); // chunk를 모으는 것이 끝남 req . on ( 'end' , function () { // req.rawBody = data; console . log ( 'on end: ' , data ) if ( ...

JSON stringify, parse

 JSON.stringify(object) : 객체를 JSON양식의 문자열로 변형한다. let obj = { apple : 1 , banana : 2 , tomato : 3 , orange : 4 } let str = JSON . stringify ( obj ); console . log ( str ); 위 str의 콘솔로그 결과는 아래와 같이 문자열(string)로 출력된다. {     "apple" : 1,     "banana" : 2,     "tomato" : 3,     "orange" : 4 }  JSON.parse(str) : JSON양식의 문자열을 JSON양식의 객체(object)로 변형한다. let str = '{"apple" : 1, "banana" : 2, "tomato" : 3, "orange" : 4}' ; let obj = JSON . parse ( str ); console . log ( obj ); str은 위쪽의 콘솔로그에 { apple: 1, banana: 2, tomato: 3, orange: 4 } 와 같이 출력이 된다.