私はこのmongo-db.jsファイルを持っています:
var MongoClient = require('mongodb').MongoClient,
ObjectID = require('mongodb').ObjectID;
exports.MongoDB = function() {
this.DB_NAME = "myDBNAME";
this.MongoClient = MongoClient;
return this;
};
exports.MongoDB.prototype.openDB = function(action) {
console.log("Open DB");
var scope = this;
this.MongoClient.connect(this.generateMongoUrl(), function(err, db) {
if (!err) {
console.log("Open DB Success");
if (action && typeof action === "function") {
action(db, scope);
}
} else {
console.log("DB Connect Error: " + err);
}
});
};
exports.MongoDB.prototype.closeDB = function(db, action) {
var scope = this;
return function() {
console.log("Close DB and apply action with arguments");
db.close();
action.apply(this, arguments);
};
};
exports.MongoDB.prototype.getItemById = function(id, callback) {
this.openDB(function(db, scope) {
console.log("Retrieving item: " + id);
db.collection("items", function(err, collection) {
if (err) { callback(err); }
else {
collection.findOne({ _id: ObjectID.createFromHexString(id) }, scope.closeDB(db, callback));
}
});
});
};
exports.MongoDB.prototype.getAllItems = function(callback) {
this.openDB(function(db, scope) {
console.log("Retrieving all items");
db.collection("items", function(err, collection) {
if (err) { callback(err); }
else {
collection.find({ }).toArray(scope.closeDB(db, callback));
}
});
});
};
そして、私は以下を実行します:
var scope = this;
var _items = [];
var counter = 0;
var done = function() {
console.log("done!");
};
var getItem = function(error, item) {
if (error) { }
else {
console.log("done loading item " + item._id);
_items.push(item);
if (_items.length === counter) {
done();
}
}
};
this.db.getAllItems(function(error, items) {
if (error) { }
else {
for (var i = 0; i < items.length; i++) {
var id = items[i]._id;
if (id) {
counter++;
console.log("load item " + id);
scope.db.getItemById(id, getItem);
}
}
}
});
このコードを実行すると、次のようになります。
Open DB
Open DB Success
Retrieving all items
Close DB and apply action with arguments
load item 50fb26263d47b70000000001
Open DB
load item 50fb277f172a5d0000000001
Open DB
load item 50fb2aa7865d870000000001
Open DB
load item 5102b7ddfe581ce5c7000001
Open DB
load item 5109678839aefde9fe000001
Open DB
load item 51096a91d0b50572ff000001
Open DB
load item 51096b06405d398bff000001
Open DB
load item null
Open DB
load item 51098b6b58bc1d0000000001
Open DB
load item 51098e16fb0e710000000001
Open DB
load item 51098e31a725100000000001
Open DB
load item 510991f20bc7690000000001
Open DB
load item 51099261258c710000000001
Open DB
load item 5109928b7edf7a0000000001
Open DB
load item 510992f0c73ccc0000000001
Open DB
load item 51099336e8a0090000000001
Open DB
load item 5109938d5fc9ce0000000001
Open DB
load item 510993cc8159610000000001
Open DB
load item 51099530ab74fb0000000001
Open DB
load item 5109956e8b059e0000000001
Open DB
load item 510995965f38da0000000001
Open DB
load item 510995ca14c0610000000001
Open DB
load item 5109963f2acd750000000001
Open DB
load item 5109966fc7001b0000000001
Open DB
Open DB Success
Retrieving item: 5109928b7edf7a0000000001
/myproj/node_modules/mongodb/lib/mongodb/connection/server.js:481
throw err;
^
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
at Function.createFromHexString (/myproj/node_modules/mongodb/node_modules/bson/lib/bson/objectid.js:212:11)
at exports.MongoDB.getItemById (/myproj/js/mongo-db.js:95:52)
at Db.collection (/myproj/node_modules/mongodb/lib/mongodb/db.js:496:44)
at exports.MongoDB.getItemById (/myproj/js/mongo-db.js:92:12)
at exports.MongoDB.openDB (/myproj/js/mongo-db.js:72:17)
at MongoClient.connect (/myproj/node_modules/mongodb/lib/mongodb/mongo_client.js:112:5)
at _finishConnecting (/myproj/node_modules/mongodb/lib/mongodb/db.js:2126:7)
at Db.open (/myproj/node_modules/mongodb/lib/mongodb/db.js:249:14)
at Server.connect.connectCallback (/myproj/node_modules/mongodb/lib/mongodb/connection/server.js:277:7)
at g (events.js:192:14)
console.log(typeof id);
の前に置くObject.createFromHexString(...)
と、Object が得られます。
ID を変更しようとするとid=id+"";
、同じエラーが発生しました。
なぜこのエラーが発生するのですか?