Work with cloudant data

Howdy folks,

I am only fairly new to node-red and to cloudant. Must say, starting to really like it though.
Anyhow, I have a fairly basic flow with a http input -> function -> cloudant search index ->function (here is the problem) ->http output

So my question is, how on earth do I work with the output from cloudant?
Using msg.payload.length I can find out the length, but I can't get any deeper into the data and it is starting to send me a little crazy. I have a programming brain and can't believe I am having trouble with this.... just need things to get a little more difficult for me to understand things I think.

Anyhow, some assistance would be absolutely awesome :slight_smile:
Karen

Edit: just adding on, I think I know roughly where the issue is. when looking at debug, under array it has 0: object. If the 0 was a word then I am guessing I should be able to do msg.payload.someword.everything else. Unfortunately, I can't seem to be able to do msg.payload.0.everythingelse
Any clues?

Many database queries return an array of objects, which is what you seem to be describing -- this is analogous to the array being the results set, and each object being a "row" of data.

However, the only way for us to help you is to have the actual data coming out of the cloudant search. In the debug sidebar, when you hover the mouse over the msg.payload array you will see some little buttons appear on the right. Use the "copy" button to place the data (as a JSON string) onto the clipboard. Then you can post that data in this discussion thread -- just be sure to place the text into a code block. This forum expects data like this to have a line with only 3 backtics (i.e. ```) before and after the data.

In the meantime, you may want to read through the "working with messages" wiki page for more examples and information.

{"_id":"eb85c71cf6b1373dc8376964f1a6e3fe","_rev":"1-363ac7ee2acc17d5b50078d7fad267da","name":"track6","type":"Feature","geometry":{"type":"Polygon","coordinates":[[[152.956398,-27.058114],[152.954621,-27.057849],[152.954548,-27.058211],[152.954783,-27.058974],[152.953764,-27.05985],[152.954684,-27.060275],[152.955117,-27.059761],[152.955514,-27.059737],[152.957616,-27.061047],[152.958726,-27.059536],[152.959186,-27.056965],[152.959664,-27.055471],[152.959962,-27.054884],[152.960404,-27.054531],[152.960052,-27.054298],[152.957823,-27.053366],[152.956957,-27.053261],[152.956037,-27.053695],[152.955712,-27.055792],[152.956335,-27.056041],[152.9561,-27.057391],[152.95647,-27.057536]]]}}

You should be able to reference, for example, msg.payload.name which should be "track6". Similarly msg.payload.geometry.coordinates should be the array of coordinates. The debug window has a very useful feature that allows you to easily find the path of a value. If you hover over one of the values some little buttons appear on the right hand side. Clicking one of those will popup Path Copied and then you can paste that into a function node or wherever.

This is telling you that msg.payload is an array and the first element of the array is the object containing the document from cloudant.

So that means you use msg.payload[0] to access that first element of the payload array.

@shrickus has already linked to the documentation that shows how you can identify the proper path to any message element. That can really help understand the structure of your messages and how to access it's elements.

Hi,
Thanks, while I was nicely waiting for my message to appear after being put on hold I finally figured it out. Yes it was msg.payload[0] that got it :slight_smile:

As a follow on from that, has anyone done anything with the geospatial index? It seems on bluemix there isn't the api button on the cloudant dashboard so I am having a little trouble getting the next part of my puzzle to work out... that said, I fight with things for a bit, ask a question, then when I am waiting for the reply i seem to figure it out... maybe that might work again

2 Likes