2

I have Couchbase notification documents stored in below format:

{
    "_id":"notification::5403cb1a6c965dcfe9",
    "alert":"test data",
    "body":{
        "created_by":"user_54986e326694f10e511cf",
        "description":" reported ",

    },
    "channels":[
        "user_54986e326694f10e511cf",
        "user_54986e326694f10e511cp"
    ],
    "notification_type":"new_report",
    "seen":true,
    "timestamp":"2015-01-06T15:48:25.092Z",
    "type":"notification"
}

The JSON document contains channels array, it is a list of user_id who subscribe to channels. Problem is I have to emit notification document if chaneel field contain user_id, if I pass user_id ='user_54986e326694f10e511cf' pass as key parameter.

Below is my Couchbase view map function but it doesn't return anything

function (doc, meta) {
  if(doc.type=='notification' && doc.channels){

        for (var user in doc.channels) {
            emit(user, {
                alert: doc.alert,
                body: doc.body,
                seen: doc.seen,
                timestamp: doc.timestamp
            });

        }

    }
}

I am wondering where i am doing wrong.

DaveR
  • 9,540
  • 3
  • 39
  • 58
rash111
  • 1,307
  • 4
  • 19
  • 35

1 Answers1

1

Those "alert", "body", "seen" etc that you're emitting as keys in the dictionary - are those strings? if so, shouldn't they be quoted?

In other words, try this:

function (doc, meta) {
    if(doc.type=='notification' && doc.channels){

        for (var user in doc.channels) {
            emit(user, {
                "alert": doc.alert,
                "body": doc.body,
                "seen": doc.seen,
                "timestamp": doc.timestamp
            });
        }
    }
}
DaveR
  • 9,540
  • 3
  • 39
  • 58
FuzzyAmi
  • 7,543
  • 6
  • 45
  • 79
  • i tried this, but still get {"total_rows":225784,"rows":[ ] } – rash111 Sep 03 '15 at 05:29
  • lets break it down to pieces. You should first make sure that your function works well - meaning that it outputs something at all. And only later you should use a query to limit that output. I suggest you use the couchbase web UI to see the result of your view (not your code). What output are you getting form it? there should be a link that opens the view results in another tab and prints out 10 of the results by default. what do you see? – FuzzyAmi Sep 03 '15 at 06:12
  • I am using couchbase web UI, when i run this query without any key parameter it gives result, after then i use key from result set and try to run agian it gives {"total_rows":225784,"rows":[ ] } – rash111 Sep 03 '15 at 07:12
  • can you provide an example of the output? just a line or two from your view's results? – FuzzyAmi Sep 03 '15 at 07:23
  • result when i didnt use any key:- { "total_rows":220, "rows":[ { "id":"notification::53aef3f78cb80cdb602", "key":"0", "value":{ "alert":"smeagol reported \"hot\" at Demo All Manager", "body":{ "created_by":"user_52ca436db68ec76a33a26bc5", "description":" reported ", "summary":"hot", "username":"smel" }, "channels":[ "user_53a06ba16694f16de1b9cb21" ], "seen":false } } ] } when i use key user_53a06ba16694f16de1b9cb21 it result :- {"total_rows":225784,"rows":[ ] } – rash111 Sep 03 '15 at 07:29
  • it looks like the key emitted is `"0"` and not `"user_53a06ba16694f16de1b9cb21"` as you expected, perhaps a problem with your map function? – Simon Baslé Sep 03 '15 at 08:05