문제

I have a CouchDB database (v1.2.0) with documents like:

{
   "_id": "pages/1",
   "_rev": "15-56ad5a5e879206edb04a7a62105dd25d",
   "content": "<html lang=\"en\"><head><title>Page Title</title></head></html>"
}

According to this article I should simply write a view like this one:

// by_lang
function(doc) {
    var html = new XML(doc.content);
    emit(html.@lang, {title: html.head.title.text()});
}

, to get data from the XML.

Unfortunately that doesn't work. At first I though that the CouchDB binaries I've downloaded don't include SpiderMonkey which includes E4X, but that is not the case. If I add a log(html); statement I can see the XML been parsed correctly (@ the Erlang window).

What the Erlang window states that is wrong with my code is this:

Log :: function raised exception (new TypeError("String.prototype.toJSON called on incompatible XML", "../share/couchdb/server/main.js", 1138)) with doc._id pages/1

I opened the main.js file and the code that breaks is the following:

// moving this away from global so we can move to json2.js later
toJSON : function (val) {
    return JSON.stringify(val);
},

Googling for that error brought me to this page which mentions json2.js (which is also mentioned in the remark in the code above.

Any ideas how to solve this? Is there any chance that this is a bug, or am I doing something wrong here?

도움이 되었습니까?

해결책

I think the problem is that toJSON couldn't convert whatever the html.@lang object is (it's not a plain string until you call .toString()

So, change your emit to include toString() calls (note that I also changed the .text() call to a .toString() call:

emit(html.@lang.toString(), {title: html.head.title.toString()});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top