2017-03-27 104 views
0

當我嘗試使用geocoder,geojson和rails進行API調用時出現此錯誤。這是錯誤:ruby​​ on rails,mongoid和Geocoder

Showing /Users/<User>/Sites/<Sites>/<Site>/app/views/api/exam_centers/index.json.rabl where line #2 raised: 

error processing query: ns=db-<site>.exam_centers limit=30Tree: GEONEAR field=coordinates maxdist=0.00313922 isNearSphere=1 
Sort: {} 
Proj: {} 
planner returned error: unable to find index for $geoNear query (2) 

我不明白這個問題。 這裏是我的代碼:

的JavaScript/api.js

function api_get_exam_centers_by_coordinates(latitude, longitude, onSuccess, radius) { 
    var url = '/api/exam_centers?latitude='+latitude+'&longitude='+longitude; 
    if (radius) url += '&radius='+radius; 
    $.getJSON(url, onSuccess); 
} 

控制器/ API/exam_centers_controller.rb

class Api::ExamCentersController < ApplicationController 
    caches_action :index, :cache_path => Proc.new {|c| c.request.url }, :expires_in => 1.day, :unless => proc { params[:latitude].present? } 

    def index 
    if params[:latitude].present? && params[:longitude].present? 
     radius = if params[:radius].blank? 
       20 
       else 
       params[:radius].to_f/1000 
       end 
     @center = [params[:latitude], params[:longitude]] 
     @exam_centers = ExamCenter.near([params[:latitude], params[:longitude]], radius, units: :km).limit(30) 
    end 
    end 

    def show 
    @exam_center = ExamCenter.find params[:id] 
    end 
end 

的意見/ API/exam_centers/base.geojson .rabl

node :type do 
    'Feature' 
end 

node :geometry do |examcenter| 
    { 
    type: 'Point', 
    coordinates: examcenter.coordinates 
    } 
end 

node :properties do |examcenter| 
    data = {} 

    [:id, :title, :adress, :zipcode, :city, :type, :link].map do |field| 
    data[field] = examcenter.send field 
    end 

    data[:is_la_poste] = examcenter.isLaPoste? 
    data[:is_sgs] = examcenter.isSGS? 
    data[:is_codengo] = examcenter.isCodengo? 

    data.keep_if {|key, value| value.present?} 
end 

視圖/ API/exam_centers/index.json.rabl

object false 

node :type do 
    'FeatureCollection' 
end 


child @exam_centers => :features do 
    extends 'api/exam_centers/base' 
end 


#collection @exam_centers 

#extends 'api/exam_centers/show' 

視圖/ API/exam_centers/show.json.rabl

object @exam_center 

attributes :id, :coordinates, :title, :adress, :zipcode, :city, :type, :link 

視圖/ exam_centers /索引.html.erb

<div class="map-container"> 

    <div id="map-exams" class=" hidden-xs hidden-sm" style="<% if [email protected]? && params[:city].blank? %> display: none !important;<% end %>" > 

    <div class="map-refresh-controls" style="<% if [email protected]? && params[:city].blank? %>display: none !important;<% end %>"> 
     <a id="map-refresh-btn-exam" class="btn btn-primary btn-large hide "> 
     <%= fa_icon 'refresh' %> Relancer la recherche ici 
     </a> 
     <div class="map-auto-refresh-exam hide"> 
     <div class="checkbox"> 
      <label> 
      <input class="map-auto-refresh-checkbox" type="checkbox"> 
      Rechercher quand je déplace la carte 
      </label> 
     </div> 
     </div> 
     <div class="map-loader"><i class="icon-spinner icon-spin icon-large"></i> Chargement... </div> 
    </div> 
    </div> 


    <div id="navRightWrapper" style="<% if [email protected]? && params[:city].blank? %> width: 100% !important;<% end %>"> 
    <form class="search-form"> 
     <div id="navRight-search-box" class="navRight-section input-group"> 
     <span class="input-group-addon" id="basic-addon1" style="height: 46px !important; padding-top: 14px !important;"><%= fa_icon 'search' %></span> 
     <input id="search-box-exam-2" class="form-control input-lg dept_list" placeholder="Où cherchez-vous un centre d'examen" type="text" /> 
     <!-- suppression id="input_home_large" car --> 
     </div> 
    </form> 
    <div id="navRight-filter-box" class="navRight-section" style="margin-top: 10px !important;"> 
     <span style="margin-right: 7px">Trier par</span> 
     <div class="btn-group" role="group"> 
     <button class="btn btn-default" type="button" id="sorting_by_la_poste">La Poste </button> 
     <button class="btn btn-default" type="button" id="sorting_by_sgs">SGS</button> 
     <button class="btn btn-default" type="button" id="sorting_by_codengo">Codengo </button> 
     </div> 
    </div> 
    <div id="navRight-middle"> 
     <div class="exams-loader hide"><i class="icon-spinner icon-spin icon-large"></i> Chargement...</div> 
     <div id="listResultsExams"> 
     <%= render partial: 'exam_centers/exam_center', collection: @exam_centers %> 
     </div> 
    </div> 
    </div> 

</div> 

<script id="ExamCenterListTpl" type="text/template"> 

    {{#features}} 

    <div class="entry-line-small" data-position="{{geometry.coordinates}}" data-distance-from-center="{{properties.distance_from_center}}" data-marker="{{properties.title}}" data-name="{{properties.title}}" data-is-la-poste="{{properties.is_la_poste}}" data-is-sgs="{{properties.is_sgs}}" data-is-codengo="{{properties.is_codengo}}"> 
    <div class="row"> 
     <div class="entry-line-small-text tiny col-md-8 col-sm-8"> 
     <div class="entry-line-small-title"> 
      <h6 class="title"><a class="exam-centers-listing-title" href="#" data-position="{{geometry.coordinates}}" data-marker="{{properties.title}}">{{properties.title}}</a></h6> 
      <div class="listing-address"> 
      {{properties.adress}} {{properties.zipcode}} {{properties.city}} {{properties.type}} 
      </div> 
     </div> 
     </div> 
    </div> 
    </div> 
    {{/features}} 

</script> 

<script id="ExamCenterPopupTpl" type="text/template"> 
    <strong><a id="exam-center-title-popup-link" href="#">{{ title }}</a></strong> 
    <br /> 
    <div class="listing-address" style="line-height: 11px"> 
    {{ adress }} {{ zipcode }} {{ city }} {{ type }} 
    </div> 
</script> 


    <%= javascript_include_tag "mapbox.js" %> 

有人知道這個錯誤是什麼?

回答

1

要使用geoNear您需要在集合中使用地理空間索引。

檢查,你有你的模型的良好index爲:

https://docs.mongodb.com/ruby-driver/master/tutorials/6.1.0/mongoid-indexes

class Person 
    include Mongoid::Document 
    field :location, type: Array 

    index({ location: "2d" }, { min: -200, max: 200 }) 
end 

你也應該運行rake任務bin/rake db:mongoid:create_indexes創建索引。 你也可以在你的rails控制檯裏嘗試Mongo查詢。

+0

謝謝你隊友!它工作得很好! –