【问题标题】:Stack Level Too deep error for google maps谷歌地图的堆栈级别太深错误
【发布时间】:2020-10-01 15:21:22
【问题描述】:

我得到这个堆栈级别太深的错误,我不确定它来自哪里。这是错误:

我正在使用 google maps javascript api 来显示项目。这是模型:

 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

这里是触发错误的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>

这里是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);

});

我相当确定错误来自模型,但我对所有事情仍然有些陌生。任何帮助将不胜感激! :)

【问题讨论】:

  • 您可以使用一些试错法来查明错误。注释掉各种代码(例如,暂时将 address 方法替换为返回假字符串的方法)并查看哪些更改可以消除错误。
  • 感谢您的最大回答!我意识到它是什么

标签: ruby-on-rails ruby ruby-on-rails-5.2


【解决方案1】:

原来是“to_json”方法导致了问题。

在我的案例中,这个问题的根本原因是在我的模型(表中的列)中有一个与附件同名的字段,这不是必需的,因为 Active Storage 使用单独的表。而当在这样的模型对象上调用 to_json 时,会导致 Stack level too deep 错误。从数据库中删除该列后,问题就消失了。

This post really helped my problem

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 2013-10-05
    相关资源
    最近更新 更多