2

I Created simple couchbase spatial view to get list of report, but it gives {error: "error", reason: "badarg"} error.

Here is my view:-

function (doc, meta) {
    if (doc.type == "report" && doc.location && doc.location.type && doc.location.coordinates) {

          if(doc.location.type=='Point'){ 
             emit(doc.location, {
               summary:doc.summary
              });
          }
    }
}

why this badarg error is coming, same view work on different doc.type

rash111
  • 1,307
  • 4
  • 19
  • 35
  • does it work when you run it in the UI? – FuzzyAmi Aug 17 '15 at 17:15
  • no, it didn't work in the UI – rash111 Aug 18 '15 at 05:42
  • What error are you getting via the UI? – FuzzyAmi Aug 18 '15 at 06:02
  • {error: "error", reason: "badarg"} – rash111 Aug 18 '15 at 08:34
  • Can you please post an example document – kruthar Aug 18 '15 at 18:54
  • here is report doc { "_id":"report::54aee49b8ca7222600000000", "_rev":"87-038a70dd74a1c5fcaab2065d383aeef7", "agree_allowed":true, "agrees":0, "archived":true, "assigned_by":"", "assigned_to":"", "comments_allowed":true, "email":"ana46@gmail.com", "gps_enabled":true, "location":{ "coordinates":[ "-71.188954", "42.365682" ], "type":"Point" }, "num_comments":14, "off_site":false, "resolved":"false", "status":".", "summary":"hot", "time_open":0, "timestamp":"2015-01-08T20:12:11.368Z", "type":"report", "user_id":"user_54aee35c6694f145f7c05", "username":"ana46", } – rash111 Aug 19 '15 at 06:02

1 Answers1

0

I used your View on an empty bucket and used the example query on the View UI page to test it:

_design/dev_test/_spatial/test?stale=false&connection_timeout=60000&limit=10&skip=0

On an empty bucket I got a successful but empty result, which was expected. As soon as I added the example document above, then I get your same error:

    {error: "error", reason: "badarg"}

After some reducing of the View and of the document I found that with this particular document it fails because there are double quotes around the latitude and longitude coordinates in your location field (doc.location.coordinates). They need to be unquoted numbers if you want to pass your location field in as the geometry for the key. More on spatial views can be found here: http://docs.couchbase.com/admin/admin/Views/views-geospatial.html

Ideally, you would fix your issue by fixing the documents in your bucket. Whatever is creating your location field is putting the coordinates in as strings when they need to be floats. If that is not possible then you can try something similar to the following to doing the conversion in your View, at the very least this should prove that the string coordinates are the root cause of your problem:

function (doc, meta) {
  if (doc.type == "report" && doc.location && doc.location.type && doc.location.coordinates) {
    if(doc.location.type=='Point'){ 
      for(var i=0; i<doc.location.coordinates.length; i++){
        doc.location.coordinates[i] = parseFloat(doc.location.coordinates[i]); 
      }
      emit(doc.location, {summary:doc.summary});
    }
  }
}
kruthar
  • 309
  • 1
  • 2
  • 10
  • Is there any way to handle that in views, like just check whether it is string or number – rash111 Aug 19 '15 at 13:31
  • Yes, the views are just javascript, so you can test the values in doc.location.coordinates, convert to doubles if needed, then use that as the geometry for the key. – kruthar Aug 19 '15 at 13:34
  • do you have any example of that? – rash111 Aug 19 '15 at 13:43
  • Check my post edits, to be clear the ideal solution like I said is to fix your document generation. – kruthar Aug 19 '15 at 14:04
  • 1
    when i use above for loop in view, it gives {error: "error", reason: "function_clause"} error – rash111 Aug 19 '15 at 14:17
  • So, I can't help much with just an error message, I don't know exactly what you tried to do. I added a complete edit of your View that works for me. But keep in mind I am only working with the information you have given in a very sterile environment (fresh cb install, and only one document). You'll have to do more of your own testing around this if it still doesn't work to see if this is a new problem or still related to your original problem. – kruthar Aug 19 '15 at 14:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87392/discussion-between-kruthar-and-rash111). – kruthar Aug 19 '15 at 14:27