Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot access private method #60

Closed
kiwionly opened this issue Jan 25, 2023 · 15 comments
Closed

TypeError: Cannot access private method #60

kiwionly opened this issue Jan 25, 2023 · 15 comments

Comments

@kiwionly
Copy link

kiwionly commented Jan 25, 2023

Hello,

I am using version 10.0.1 and encounter this issue when call get(), below show error in my browser console:

chunk-VNXF6W56.js?v=202f4f6a:39 Uncaught (in promise) TypeError: Cannot access private method
    at __accessCheck (chunk-VNXF6W56.js?v=202f4f6a:39:11)
    at __privateMethod (chunk-VNXF6W56.js?v=202f4f6a:47:3)
    at Proxy.get (tiny-lru.esm.js:80:7)
    at Object.<anonymous> (main.js:80:23)

which turn out to be here:

image

however, that is no issue at version 9.0.3

@avoidwork
Copy link
Owner

Hi,

What version of node are you using? it looks like i didn't bump the 'engines' value in package.json when releasing 10.0.0 which marks that method as private.

@kiwionly
Copy link
Author

kiwionly commented Apr 4, 2023

Hi, finally I had some time to show the errors, I am tiny -lru in vuex, node version 18.13.0

const store = createStore({
	
	state: {
		cache: lru(200)
	},
	
	getters: {		
		cache: (state) => (key) => {						
			return state.cache.get(key);
		},
	},

with tiny -lru version 9.0.3, it working fine. However, when I upgrade to version 10+, I encounter error like this

image

image

Hope this help !

@avoidwork
Copy link
Owner

avoidwork commented Apr 4, 2023

If you have a Proxy calling the cache, is the context of the call changing? what is "this" via breakpoint at that line (81)? it should be LRU, if it's the Proxy you want to either bind the get/set or .call() the methods; this is outside of the LRU cache entirely.

I'm assuming you're using an invalid browser or node, but you're not providing enough info to help.

If 9.x works, the context should be correct and you're using a version your rte can't run, or you're calling it incorrectly.

@avoidwork avoidwork reopened this Apr 4, 2023
@kiwionly
Copy link
Author

kiwionly commented Apr 5, 2023

I try to print out lru object and it actually look like this :

image

image

I assume the [target] should have a has or get method ? ( since I not an expert on JS field, I could only try simple way to pinpoint issue )

@avoidwork
Copy link
Owner

can you share the code that creates the proxy; the issue is the usage of the proxy.

@avoidwork
Copy link
Owner

The proxy getter needs to rewritten; however you shouldn't use a proxy on things with private fields. They're also really slow.

class Example {
    #world = "world";
    hello(){
        console.log(`Hello ${this.#world}`);
    }
}

const exampleProxy = new Proxy(new Example(),   {
    has: function (obj, prop) {
        return prop in obj;
    },
    get(target, prop, receiver) {
        return (params) => {
            return target[prop](params);
        }
    },
    set: function (obj, prop, value) {
        obj[prop] = value;
        return this;
    }
});

Screenshot 2023-04-05 075730

@avoidwork
Copy link
Owner

In the future please provide a sample like this; chance of me trying to recreate your issue is very small.

@kiwionly
Copy link
Author

kiwionly commented Apr 5, 2023

I use in vuex , a bit complicated to show code because it also had html ( My code is running in browser ).

The code roughly like this

import { createStore } from 'vuex'
import { lru } from "tiny-lru"

const store = createStore({
	
	state: {
		cache: lru(200)
	},
	
	getters: {		
		get: (state) => (key) => {						
			return state.cache.get(key);
		},
	},
	
	mutations: {		
		set(state, payload) {			
			state.cache.set(payload.key, payload.value);
		}		
	}
})

// usage, not sure if could call like `store.getters.get(key);`

var cache = this.$store.getters.get(key);

This had no issue on tiny-lru version 9.0.3 but not version above 9.0.3, the error show as comment above ( cannot access private method )

@avoidwork
Copy link
Owner

I know, because at 10.0.0 it became a private method and your proxy code doesn't handle that. So your options are to use 9.x.x or update your code to handle privates.

@kiwionly
Copy link
Author

kiwionly commented Apr 6, 2023

I see, thanks !

@avoidwork
Copy link
Owner

avoidwork commented Apr 6, 2023

Might be worth opening an issue with vuex if you can't override/patch the proxy.

@kiwionly
Copy link
Author

kiwionly commented Apr 7, 2023

I will stick to version 9 since I not have clear idea of how proxy and private method work.

@avoidwork
Copy link
Owner

I'm curious about the scope of this issue, proxies affecting interoperability wasn't a consideration when I set the field to private.

@avoidwork
Copy link
Owner

@kiwionly try 11.0.0; the private field was replaced with the same function in scope so that should work with the proxy.

@kiwionly
Copy link
Author

kiwionly commented Apr 7, 2023

@avoidwork I just upgrade to version 11 and it work as expected, thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants