简体   繁体   English

谷歌地图的堆栈级别太深错误

[英]Stack Level Too deep error for google maps

I've been getting this stack level too deep error and I'm unsure where it is coming from.我一直得到这个堆栈级别太深的错误,我不确定它来自哪里。 Here is the error:这是错误:

错误页面

I'm using the google maps javascript api to display items.我正在使用谷歌地图 javascript api 来显示项目。 Here is the model:这是模型:

 class Item < ApplicationRecord
  belongs_to :user
  has_one_attached :photo

  validates :price, presence: true, if: :available_for_rent?
  #address validations
  validates :street, presence: true
  validates :city, presence: true
  validates :state, presence: true
  validates :zip, presence: true

  scope :rentable, -> { where(available_for_rent: true) }

  #google maps
  geocoded_by :address
  after_validation :geocode

  def address
    [street, city, zip, state].compact.join(", ")
  end

  #validate size of photo uploads
  validate :photo_validation
  def photo_validation
    if photo.attached?
      if photo.blob.byte_size > 3000000
        photo.purge
        errors[:base] << 'The file uploaded was too big. Jubal Talents only allows photo uploads under 3 MB.'
      elsif !photo.blob.content_type.starts_with?('image/')
        photo.purge
        errors[:base] << 'Wrong format. Please ensure file is jpg or png.'
      end
    end
  end
  
end

Here is the html where the error is triggered:这是触发错误的html:

  <div class="column">
      <%= tag.div nil, id:'map', data: {items: @items.to_json(methods: [:address])}%>
      <div class="notification">
        <p class="title is-6">Addresses are not exact. You must contact the owner to recieve a pickup address.</p>
      </div>
  </div>
</div>

and here is the javascript:这是javascript:

document.addEventListener("turbolinks:load", function() {
    var map = new GMaps({
        div: '#map',
        lat: 38.5816,
        lng: -121.4944
      });
      window.map = map;

      var items = JSON.parse(document.querySelector("#map").dataset.items);
      window.items = items;

      var bounds = new google.maps.LatLngBounds();

      items.forEach(function(item) {
        if (item.latitude && item.longitude) {
            var marker = map.addMarker({
                lat: (item.latitude - Math.random()*.1),
                lng: (item.longitude - Math.random()*.1),
                title: item.address,
                infoWindow: {
                    content: `<p><a href="/items/${item.id}">${item.name}</a></p>`
                }
            })
        bounds.extend(marker.position);
        }
      });
    map.fitBounds(bounds);

});

I'm fairly certain that the error is coming from the model but I'm still somewhat new to everything.我相当确定错误来自模型,但我对一切仍然有些陌生。 Any help would be much appriciated!任何帮助都会非常感谢! :) :)

It turns out that the "to_json" method was causing the problem.事实证明,“to_json”方法导致了问题。

The root cause of this problem in my case was having a field with the same name in my model (column in the table) as the attachment, which is not needed, since Active Storage uses separate tables.在我的案例中,此问题的根本原因是我的模型(表中的列)中有一个与附件同名的字段,这是不需要的,因为 Active Storage 使用单独的表。 And when to_json is called on such a model object, it causes Stack level too deep error.并且当在这样的模型对象上调用 to_json 时,会导致 Stack level too deep 错误。 After removing the column from the database, the problem goes away.从数据库中删除该列后,问题就消失了。

This post really helped my problem 这篇文章真的帮助了我的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM