简体   繁体   中英

Error with Geocode, Rails and Mongoid

I'm using Rails 3.2.16, mongoid and geocode.

I've set up the model for my profile to convert the street and city address into long/lat. This is working well. I also have a search that's based on geocode - which also works. It searches for profiles with in certain distances etc.

Even when I misspell a city in the search it's intelligent enough to figure out what I actually mean.

However, when I search for something that really stumps geocode, like "sgervwsewerw", it throws the "We're sorry, but something went wrong" error. How can I prevent this error and just have it send back no results? Here is the error information:

Processing by ProfilesController#index as HTML
  Parameters: {"utf8"=>"✓", "profile_search"=>"sgervwsewerw"}
  Rendered profiles/index.html.erb within layouts/application (137.3ms)
Completed 500 Internal Server Error in 489.6ms

ActionView::Template::Error (The operation: #<Moped::Protocol::Query
  @length=208
  @request_id=2
  @response_to=0
  @op_code=2004
  @flags=[]
  @full_collection_name="tango_production.profiles"
  @skip=0
  @limit=0
  @selector={"$query"=>{"coordinates"=>{"$nearSphere"=>[NaN, NaN], "$maxDistance"=>0.0063151153689165445}}
  @fields=nil>
failed with error 16433: "point not in interval of [ -180, 180 ] :: caused by :: { 0: nan.0, 1: nan.0 }"

See https://github.com/mongodb/mongo/blob/master/docs/errors.md
for details about this error.):

Here is the code from my profile controller:

def index
 if params[:profile_search].present?
    @profiles = Profile.near(params[:profile_search], 25, :order => :distance).desc("position_order")
  else
    @profiles = Profile.desc("position_order")
  end
end

Your problem seems to be in the NaN argument in the $nearSphere for coordinates. Make sure, you provide correct values for the comparison (eg if using the users location, make sure it is available).

Hope that helps.

In your mongoid.yml initializer file you will find a line like:

# Raise an error when performing a #find and the document is not found.
# (default: true)
raise_not_found_error: false

default it's true, if you set it to false you will not get this error anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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