Node.js NodeJS+;MongoDB+;Express-查询不产生任何结果
一直在处理NodeJS+MongoDB+Express堆栈,遇到了执行查询的问题。我的index.js文件中有一些助手方法:Node.js NodeJS+;MongoDB+;Express-查询不产生任何结果,node.js,mongodb,express,Node.js,Mongodb,Express,一直在处理NodeJS+MongoDB+Express堆栈,遇到了执行查询的问题。我的index.js文件中有一些助手方法: var mongoClient = new MongoClient(new Server(mongoHost, mongoPort)); //B mongoClient.open(function(err, mongoClient) { //C if (!mongoClient) { console.error("Error! Exiting... Mus
var mongoClient = new MongoClient(new Server(mongoHost, mongoPort)); //B
mongoClient.open(function(err, mongoClient) { //C
if (!mongoClient) {
console.error("Error! Exiting... Must start MongoDB first");
process.exit(1); //D
}
var db = mongoClient.db("trackr"); //E
collectionDriver = new CollectionDriver(db); //F
});
app.use(express.static(path.join(__dirname, 'public')));
app.get('/:collection', function(req, res) { //A
var params = req.params; //B
collectionDriver.findAll(req.params.collection, function(error, objs) { //C
if (error) { res.send(400, error); } //D
else {
if (req.accepts('html')) { //E
res.render('data',{objects: objs, collection: req.params.collection}); //F
} else {
res.set('Content-Type','application/json'); //G
res.send(200, objs); //H
}
}
});
});
app.get('/:collection/:entity', function(req, res) { //I
var params = req.params;
var entity = params.entity;
var collection = params.collection;
if (entity) {
collectionDriver.get(collection, entity, function(error, objs) { //J
if (error) { res.send(400, error); }
else { res.send(200, objs); } //K
});
} else {
res.send(400, {error: 'bad url', url: req.url});
}
});
app.get('/:collection/iata/:value', function(req, res) { //I
console.log("entrou");
var params = req.params;
var value = params.value;
var collection = params.collection;
if (value) {
collectionDriver.getByIata(collection, value, function(error, objs) { //J
if (error) { res.send(400, error); }
else { res.send(200, objs); } //K
});
} else {
res.send(400, {error: 'bad url', url: req.url});
}
});
这里的问题是——每当我通过GET请求指向“/collection/”路由时,一切都正常——集合中的所有项都被呈现。另外,如果我指向“/collection/entity”路由,通过提供ObjectID,也会返回特定的项。当我尝试获取/collection/iata/value的请求时,不会返回任何对象。但是,当我在MongoDB提示符中执行相同的查询时,文档成功返回
整个集合查询
按对象ID查询
按IATA文档属性查询
直接通过MongoDB提示符查询
在我的index.js中,我从collectionDriver.js调用函数,代码摘录如下:
CollectionDriver.prototype.findAll = function(collectionName, callback) {
this.getCollection(collectionName, function(error, the_collection) { //A
if( error ) callback(error);
else {
the_collection.find().toArray(function(error, results) { //B
if( error ) callback(error);
else callback(null, results);
});
}
});
};
CollectionDriver.prototype.get = function(collectionName, param, callback) { //A
console.log("Get by ObjectID");
this.getCollection(collectionName, function(error, the_collection) {
if (error) callback(error);
else {
var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$"); //B
if (!checkForHexRegExp.test(param)) callback({error: "invalid id"});
else the_collection.findOne({'_id':ObjectID(param)}, function(error,doc) { //C
if (error) callback(error);
else callback(null, doc);
});
}
});
};
CollectionDriver.prototype.getByIata = function(collectionName, attribute, callback) { //A
console.log('entrou no driver - encontrar por atributo - collectionName: ' + collectionName);
this.db.collection(collectionName, function(error, the_collection) {
if( error ) callback(error);
else the_collection.find({'iata': attribute}, function(error, collection) {
collection.count({}, function(error, numDocs){
console.log(numDocs);
});
});
});
};
我知道ObjectID查询需要转换为BSON—我在尝试查询文档的属性时是否缺少类似的要求
提前感谢。您不会从getByIataprototype返回任何内容
CollectionDriver.prototype.getByIata = function(collectionName, attribute, callback) { //A
console.log('entrou no driver - encontrar por atributo - collectionName: ' + collectionName);
this.db.collection(collectionName, function(error, the_collection) {
if( error ) callback(error);
else the_collection.find({'iata': attribute}, function(error, doc) {
if (error) callback(error);
else callback(null, doc);
});
});
};
你确定你的路线不会发生碰撞吗?也许/collection/iata/value与/:collection/:entity路径匹配。你是对的,愚蠢的错误。为下面的代码替换了getByIata原型,它工作了:CollectionDriver.prototype.getByIata=function(collectionName,attribute,callback){//A console.log(“Get by Iata”);this.getCollection(collectionName,function(error,the_collection){if(error)callback(error);else{the_collection.findOne({'iata':属性},函数(error,doc){//C if(error)回调(error);else回调(null,doc);});}};