Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FATAL ERROR: v8::Object::SetInternalField() Internal field out of bounds on Node js using node-cache

I have a api export with a lot of data so I tried to used the node-cache so the api will be faster since take more than 2 min to get the data, I am new on this so I found the follow code: Notes: env node js/express

const NodeCache = require("node-cache");
const axios = require('axios');
const myCache = new NodeCache({stdTTL:100000})
 axios.get('http://localhost:5000/test/example)
        .then(function (response) {
            console.log(response)
            myCache.set("exampleCache",response,10000);
            res.send(response)

Any suggestion or recommend will be great!

like image 468
Rafa Soto Chan Avatar asked Jan 27 '26 20:01

Rafa Soto Chan


2 Answers

By default, the NodeCache module clones the object before saving it.

In your case, you're trying to cache the Axios response object, which cannot be cloned by NodeCache (it's a big and complex object), so you're getting the error.

Instead, I recommend cloning the HTTP response data (response.data), which is much smaller and clonable. Or, you can clone the whole Axios response object, like in your code example, but you need to set the option {useClones: false} during initialization of NodeCache, this way the NodeCache will save just the reference to the object.

like image 125
Osoian Marcel Avatar answered Jan 29 '26 10:01

Osoian Marcel


I was getting the same error, what I did was to stringify the content before saving it and parse it after retrieval. It worked like a charm.

like image 37
Prince Avatar answered Jan 29 '26 08:01

Prince



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!