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:

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.


You should be able to reference, for example, 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.

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