Your error says that the view is not defined. Somewhere in the code that you DIDN'T post, you should define it or store it in the correct variable. In the posted code, the view is stored in a variable called vista I guess as you call vista.getResolution();
.
Otherwise, if you are looking for help with the code that does not work, you should post the code that does not work, not the working example:)
EDIT:
It should be const viewResolution = /** @type {number} */ (vista.getResolution());