Decode the JSON serialized format generated by CircularJSON

I have a JSON object in my node.js code that contains circular references. To send this information to the browser, I utilized the NPM package circular-json to stringify the object and serialize the circular references:

var CircularJSON = require("circular-json");
app.get("/api/entries", function(req, res){ 
  contentClient.entries({"content_type": "xxxxxxxxxxx"}, function(err, entries){
    if (err) throw err;
    res.send(CircularJSON.stringify(entries));
  });
});

This method works effectively and transmits serialized data via res.send(); Now, I need to make the circular references workable in my frontend Angular code. One of the serialized fields in the object appears like this on the client side: "~0~fields~twitter~1"

I attempted the following steps in the browser:

  1. Copied a version of circular-json.js to my frontend site
  2. Linked the script in my index.html as follows:
    <script src="/framework/lib/circular-json.js"></script>
  3. Defined the CircularJson variable like this:
    var CircularJSON = window.CircularJSON;
  4. Parsed the incoming JSON like this:

    $http.get("/api/entries").then(function(res){
      console.log(CircularJSON.parse(res.data[0]));
    });

However, I encounter the error message: SyntaxError: Unexpected token o

Answer â„–1

If you only need to make a one-time call, consider using $http in the following manner:

function appendTransform(defaults, transform) {

  // It is not guaranteed that the default transformation is an array
  defaults = angular.isArray(defaults) ? defaults : [defaults];

  // Add the new transformation to the defaults
  defaults.unshift(transform);
  return defaults;
}

$http({
  url: '...',
  method: 'GET',
  transformResponse: appendTransform($http.defaults.transformResponse, function(value) {
    return CircularJSON.parse(value);
  })
});

Alternatively, for every call, you can set up interceptors. Examples can be found here: https://docs.angularjs.org/api/ng/service/$http

UPDATE: Correction - unshift does not return the array. The code has been modified and should now function correctly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

WebRTC error encountered: Unable to add ICE candidate to 'RTCPeerConnection'

Encountering a specific error in the browser console while working on a project involving p2p video chat. The error message is Error: Failed to execute 'addIceCandidate' on 'RTCPeerConnection': The ICE candidate could not be added.. Int ...

Express app not appearing in browser when using PM2

Utilizing PM2 within my Express application for process management and load balancing. Following the installation of PM2, I ran the command pm2 start bin/www which executed successfully in the command prompt, displaying the expected result: However, upon ...

Unraveling the Mysteries of Node.js Application Logging

We've been having smooth sailing with our Kube cluster on AWS, but there seems to be a recurring issue with our Node app crashing and generating repetitive logs from our instances... I've scoured the internet for solutions but haven't had m ...

Include the script tags retrieved from an array

I'm exploring the idea of dynamically adding JavaScript to the DOM using an array and a loop. The goal is to load each script sequentially, starting with scripts[0], then scripts[1], and so on. var myScripts = [["external", "https://code.jquery.com/j ...

Node.JS product creation triggers a Stripe outage

Attempting to create a product in stripe has been problematic for me. Every time I make the call with stripe.products.create(), it causes lag, and shortly after my server crashes due to 'JavaScript heap out of memory'. Here is my approach: impo ...

What is the process of utilizing marked plugins within a Vue3 project?

I attempted to integrate the marked plugin into my Vue.js applications. After installing [email protected], I did not encounter any issues during compilation. However, when I viewed the contents in the browser, nothing appeared. My Vue project was built u ...

Having trouble locating module '@mdx-js/mdx' in Gatsby forest

Since the most recent update today, I've been encountering this error. There is no MDX being used in my project at all.. When running npm run develop, this issue arises. Does anyone have any insights on this? internal/modules/cjs/loader.js:979 thro ...

Having trouble extracting JSON data from an API in a Flutter application

My goal is to retrieve and parse JSON data from a REST API in node.js using express and MongoDB, and then present it as userdata. However, no matter what I attempt, I am unable to successfully parse the data. It either throws an exception like "Unhandled E ...

Vue js is throwing an error because it is unable to find the "buscador" property or method that is being referenced in the render function

I am currently diving into the world of laravel combined with Vue js. I am working on integrating a search engine using vue js components, and I would greatly appreciate any help you can provide. Thank you in advance. Below is the Vue js component where t ...

What methods does a node server use to identify a request?

I have encountered an unusual situation while using a node server. Initially, I set up two express http node servers - server1 to render pages and server2 to handle business logic services accessed by server1. When users interact with server1 in their bro ...

Tips for calculating the canvas-relative mouse position on a CSS 3D transformed canvas

Recently decided to challenge myself by experimenting with drawing on 3D transformed canvases. After some trial and error, I managed to get it partially working. const m4 = twgl.m4; [...document.querySelectorAll('canvas')].forEach((canvas) =& ...

Retrieve the row based on the user_id and also include any rows that have the same event_id as that entry

I have a table with the following structure: _______________________________ | event_id | user_id | username | |-------------------------------| | 30 | 1 | user1 | | 30 | 2 | user2 | | 30 | 3 | user3 | | 31 ...

Stacking items when clicked on with jQuery UI Draggable

Can a draggable's stack procedure be activated by clicking instead of just dragging? I came across this solution, which essentially replicates the library code (with an important addition I included below). Is there a more elegant approach to achieve ...

Having trouble getting req.files to work in a Node.js Express application?

Hello there, I'm facing an issue with accepting an uploaded file. Every time I call req.files, it comes out as undefined. I can't seem to figure out what I am doing wrong... Below is a snippet of my app.js file: var express = require('expr ...

Develop a novel function

I am working on a new Order page where users can select a category, then a service, and fill out the quantity field for their order. I have an idea to create a function using @if and @else statements. In this function, I want to display the quantity fiel ...

Error encountered during npm installation - EPERM: Operation not allowed

Today's challenge began when attempting to run the angular4 project and encountering this error: npm install eperm operation not permitted. In an effort to resolve it, I decided to delete the node modules folder and try again. However, upon running np ...

Developing with Angular 1.4.8 and JavaScript involves the process of building a constructor function to inherit properties into a third object

DEVELOPER TOOLS Using Angular 1.4.8 and lodash QUERY: REVISIT To clarify my query: Create an object (articles) Apply a constructor Import the properties of a third object, but place it in proto folder to prevent cluttering the root with a large colle ...

The type '_InternalLinkedHashMap<String, dynamic>' is incompatible with the type 'BanarModel'

There seems to be an issue with the code. I believe it is because I stored an object in a list, so how can I solve this? The error message says '_InternalLinkedHashMap' is not a subtype of type 'BanarModel'. class Home ...

NodeJS API development tools for production deployment

I am new to NodeJS and currently working on a NodeJS API project that is hosted on Render with node_env in production. However, when running the project in a cloud environment, I encountered the following error: "Error: Cannot find module 'mor ...

React - warning - Avoid using <Link> outside of a <Router> context

Warning: Invariant failed: It is not recommended to use <Link> outside of a <Router> Encountering this while attempting to write a test for a component where test("renders post list div ", () => { const posts = [ { created: ...