zebralamy
zebralamy

Reputation: 333

How do i check javascript variable on Rails?

var map = new naver.maps.Map('map', mapOptions);

var latlng = map.getCenter();
console.log("latlng variable is ", latlng);

I'm using an external API (NAVER map, which is similar to Google map I believe) which runs on Javascript and I'm looking for a way to check a Javascript variable, to debug.

I want to know the value inside latlng but I can't see any log on my terminal. I googled and read that passing Javascript variable to ruby controller cannot be done directly. However, I'm not trying to pass the variable - I just want to see what's in it.

How can I do this? I can really use some help.

Upvotes: 0

Views: 270

Answers (2)

Azlan
Azlan

Reputation: 1

Please try rewrite your 'console.log' like this:

var latlng = map.getCenter();
console.log("latlng variable is "+ latlng);

Change ',' to '+'. Then see if you see any latlng value on browser log

Upvotes: 0

Tom Lord
Tom Lord

Reputation: 28305

Open the browser console. The method for this varies between operating system and browser, but for example on Chrome you can press Ctrl+Shift+J (Windows / Linux) or Cmd+Opt+J (Mac).

In general, you should be able to do something like: Right click --> inspect --> console.


Your JavaScript code above is running on the client side, i.e. in the browser itself. In order to see any log messages, you need to look at the browser log.

This is in contrast to the rails code, which is running on the server side. Rails logs appear on the server itself, because that code is not executed by the browser.

I can't see anything wrong with your code, given the information provided. Take a look at the browser log, to see the value of the variable. You can also, for example, set breakpoints in the code - so you wouldn't need to explicitly write console.log each time.

Upvotes: 1

Related Questions