gpt4 book ai didi

node.js - NodeJS + MongoDB + Express - 查询没有带来任何结果

转载 作者:可可西里 更新时间:2023-11-01 10:27:39 25 4
gpt4 key购买 nike

一直在研究 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... 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/”路由时,一切正常 - 集合中的所有项目都被渲染。此外,如果我通过提供 ObjectID 指向“/collection/entity”路由,也会返回特定项目。当我尝试对/collection/iata/value 发出 GET 请求时,没有返回任何对象。但是,当我在 MongoDB 提示符下执行相同的查询时,文档成功返回。

全集查询

collection_query

按 ObjectID 查询

query_objectID

按IATA文件属性查询

query_iata

直接通过MongoDB Prompt查询

query_mongo

在我的 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 - 我在尝试查询文档的属性时是否遗漏了一些类似的要求?

提前致谢。

最佳答案

你没有从 getByIata 原型(prototype)返回任何东西

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);
});
});
};

关于node.js - NodeJS + MongoDB + Express - 查询没有带来任何结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32156254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com