Hi all,
I came across a problem in my view script with the "instanceof"
operation.
Can anyone explain the cause of my problem duplicated by following
steps?
===
1. Add a document with an Array member into a database.
{
"_id": "oh_my_array",
"_rev": "2-3397837489",
"array_member": [
1,
2,
3
]
}
2. Run the following map-only view.
function(doc) {
emit("0_raw_data", doc.array_member);
emit("1_instanceof", (doc.array_member instanceof Array));
emit("2_constructor.name", doc.array_member.constructor.name);
emit("3_typeof", (typeof doc.array_member));
}
3. Get the strange value in "1_instanceof" in Futon.
(key, value) ->
("0_raw_data", [1,2,3])
("1_instanceof", false); <-- why not true?
("2_constructor.name", "Array");
("3_typeof", "object");
===
# As above, "constructor.name" is an workaround instead of instanceof.
So I don't know why "instanceof" operation returns false for the array
value.
# This means we cannot use the instanceof operation for condtion
expressions such as if( a instanceof Array){ ... }
BTW, I use CouchDB 0.9.0 (installed from the tarball on Apache) with
JS 1.7(installed from MacPorts).
And on couchjs standalone mode, the expression, 'instanceof Array'
returns true as follows:
===
mac:~ yssk22$ cat a.js
var doc = {
"_id": "oh_my_array",
"_rev": "2-3397837489",
"array_member": [
1,
2,
3
]
};
print(doc.array_member instanceof Array);
mac:~ yssk22$ /usr/local/bin/couchjs a.js
true
===
Thanks.
--
Yohei SASAKI
http://github.com/yssk22/