NextJS backend issue while dealing with http post, It throws converting circular structure to JSON error.
Here is the code stack trace which details issue
[Nest] 29840 - 01/04/2021, 2:46:31 PM ExceptionsHandler Converting circular structure to JSON
--> starting at object with constructor 'ClientRequest'
| property 'socket' -> object with constructor 'TLSSocket'
--- property '_httpMessage' closes the circle +199712ms
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'ClientRequest'
| property 'socket' -> object with constructor 'TLSSocket'
--- property '_httpMessage' closes the circle
at JSON.stringify (<anonymous>)
at stringify node_modules\express\lib\response.js:1123:12)
at ServerResponse.json \node_modules\express\lib\response.js:260:14)
at ExpressAdapter.reply node_modules\#nestjs\platform-express\adapters\express-adapter.js:24:57)
at RouterResponseController.apply node_modules\#nestjs\core\router\router-response-controller.js:13:36)
at \node_modules\#nestjs\core\router\router-execution-context.js:173:48
at processTicksAndRejections (internal/process/task_queues.js:94:5)
at async node_modules\#nestjs\core\router\router-execution-context.js:47:13
at async \node_modules\#nestjs\core\router\router-proxy.js:9:17
It took some time to figure out solution however this issue can be resolved by adding below code
this.http.post(https://api-enpont,body, headerData).pipe( map(response => response.data));
Do not forget to import { map } from 'rxjs/operators';
Related
I'm using Supertest to test my APIs in my NestJS application. My tests seem to be working fine except for my DELETE request test, which throws this error:
ERROR [ExceptionsHandler] Converting circular structure to JSON
--> starting at object with constructor 'Socket'
| property '_writableState' -> object with constructor 'WritableState'
| property 'afterWriteTickInfo' -> object with constructor 'Object'
--- property 'stream' closes the circle
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Socket'
| property '_writableState' -> object with constructor 'WritableState'
| property 'afterWriteTickInfo' -> object with constructor 'Object'
--- property 'stream' closes the circle
at JSON.stringify (<anonymous>)
Here is the test I currently have:
it('should delete the relevant summary', (done) => {
request(app.getHttpServer())
.delete(`/engine/summaries/${summary_id}`)
.set('Authorization', `Bearer ${token}`)
.expect(204)
.end(done)
});
I initially thought it was because I didn't use the end() callback with 'done' passed into it, but that doesn't seem to be the issue. This is my return statement in my API if it helps: return res.status(204).send();
I've encountered this error before but it was because I was testing my asynchronous code incorrectly. However, this doesn't seem to be the issue here (or maybe it is?) and I've already spent the entire day trying to figure it out. Any help is greatly appreciated!
I have following code in Kotlin:
sealed class ParentClass
data class ChildA(val prop: String): ParentClass()
object ChildB: ParentClass()
but when I try to serialize it into JSON with Moshi I get following Error:
Caused by: java.lang.IllegalArgumentException: Cannot serialize object declaration ChildB
Failed to serialize obj: ChildB of type: class ChildB to a map
I don't want to include full stack trace due to confidentiality, but essentially it fails on this line. I wonder if there is a way to serialize Kotlin object types into JSON and back?
https://github.com/ZacSweers/MoshiX/tree/main/moshi-sealed should support that use case, I believe.
I am trying to convert the XML message to JSON using camel router and save it into a file. Getting the XML message from the source and saving it to destination file etc are working. But when I try to convert to JSON, it did not work. I did not even throw any error/exception in logs. I am running on OSGI container
public class CamelRouter extends RouteBuilder {
#Override
public void configure() throws Exception {
from("file://C:/test/Sample.xml")
.routeId("file-to-file")
.log(LoggingLevel.INFO,"RouteID file-to-file !!!!! starting")
//From XML to JSON
.bean(CamelRouter.class, "convertXmlToJson")
.log(LoggingLevel.INFO,"From XML to JSON !!!!! Done")
.to("file://C:/test/JSONMessages")
.log(LoggingLevel.INFO,"Converted Message Saved successfully");
The bean method to convert XML to JSON convertXmlToJson is shown below
public String convertXmlToJson(String msg) {
log.info("NOW calling JSON conversion");
String jsonStr = null;
log.info("MESSAGE conversion starting : "); //After this message nothing happened
XMLSerializer xmlReader = new XMLSerializer();
log.info("MESSAGE before conversion : " + msg);
jsonStr = xmlReader.read(msg).toString();
log.info("JSON data : " + jsonObj.toString());
return jsonObj.toString();
}
Is anyone know why it is not executing the XMLSerializer portion. I tried this approach because the camel-xmljson's marshal().xmljson() call also give me the same results. Nothing happened after the xmljson() call in my camel routing.
Things that I checked are:
camel-xmljson feature up and running in OSGI
Dependencies mentioned in the Apache XmlJSON website added in my pom file, xom, camel-xmljson etc.
Am I missing anything here? Please help
The problem with your code route is that your bean component handler method resides within your route builder class, plus you invoke the bean component in a way that triggers another instantiation of that route builder class.
Personally, I would move convertXmlToJson to an appropriate utility class. That way you reduce mix of concern in the route builder and the bean component should work fine.
Alternatively, your route might work, if you invoke the bean component like this:
.bean(this, "convertXmlToJson")
I have a problem with my custom JSON deserializer.
I use Jackson to map JSON to Java and back. In some cases I need to write my own mapping.
I have an object (filter), which contains a set of another object(metaInfoClass). I try to deserialize the filter with Jackson, but I implemented an own deserializer for the inner object.
The JSON looks like this:
{
"freetext":false,
"cityName":null,
"regionName":null,
"countryName":null,
"maxResults":50,
"minDate":null,
"maxDate":null,
"metaInfoClasses":
[
{
"id":31,
"name":"Energy",
"D_TYPE":"Relevance"
}
],
"sources":[],
"ids":[]
}
My deserializer just works fine, it finds all the fields etc.
The problem is, that somehow (no idea why) the deserializer gets invoked on the rest of the JSON string, so the sources token is getting processed, and so on.
This is very weird, since I don't want to deserialize the big object, but only the inner metaInfoClass.
Even more weird: the CollectionDeserializer class keeps calling my deserializer with the json string even after it is ended. So nothing really happens, but the method gets called.
Any idea?
Thanks a lot!
I was able to find a solution.
I modified the implementation (in the deserialize method) to use to following code:
JsonNode tree = parser.readValueAsTree();
Iterator<Entry<String, JsonNode>> fieldNameIt = tree.getFields();
while (fieldNameIt.hasNext()) {
Entry<String, JsonNode> entry = fieldNameIt.next();
String key = entry.getKey();
String value = entry.getValue().getTextValue();
// ... custom code here
}
So with this approach, it was parsing only the right piece of the code and it's working right now.
I am using the as3corelib JSON library and decoding some JSON from a URLLoader request. However, I'm having issues with JSON.decode throwing an error:
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at
com.adobe.serialization.json::JSONTokenizer/nextChar()
at
com.adobe.serialization.json::JSONTokenizer()
at
com.adobe.serialization.json::JSONDecoder()
at
com.adobe.serialization.json::JSON$/decode()
at Main/drawMap() at
flash.events::EventDispatcher/dispatchEventFunction()
at
flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
My code is as follows:
private function storeAssets(e:Event):void
{
// returned variables from PHP call
var variables:URLVariables = new URLVariables(e.target.data);
assets = JSON.decode(variables.assets);
}
I have passed my JSON input into validators and it always returns as valid so I'm really scratching my head on this.
Your right in putting e.target.data into the URLVariables, as per this example: http://actionscriptexamples.com/2008/02/27/decoding-url-encoded-strings-in-a-flash-application-using-the-urlvariables-class-in-actionscript-30/
What I believe is happening is that URLVariables is decoding your entire string into an object, thus variables.assets is not in JSON format because it has already been converted. It could also be that variables.assets is not defined in the return data.
Trace out your variables.assets and see if it is null, or not in JSON format.
I would use eithervar variables:URLVariables = new URLVariables(e.target.data) or assets = JSON.decode(e.target.data) but not both at the same time.