gpt4 book ai didi

javascript - nodejs mongodb驱动的同步函数调用

转载 作者:行者123 更新时间:2023-11-30 08:08:35 25 4
gpt4 key购买 nike

我有一个处理 mongodb 数据库的开源项目。我正在尝试创建一个查询数据库以检查条目是否存在的函数。

问题是当 if_exists() 返回 true 或 false 时它返回 undefined 因为 mongodb 驱动函数是异步的。文件是Query.js我已经尝试了这里的解决方案来解决问题 What is the right way to make a synchronous MongoDB query in Node.js?但我仍然使用 get 方法得到未定义的结果。

完成这项工作的最佳方法是什么?

单元测试的输出如下:

running unit tests...
add query test
exists tests:
get: undefined
{}
should be true: undefined
get: undefined
{}
should be false:undefined
Captains Logs listening on port 3000
Captains_Logs v0.5.0-21
[ { name: 'rhcp', _id: 50cbdcbe9c3cf97203000002 } ]
[ { name: 'os', _id: 50cbdcbe9c3cf97203000001 } ]

您可以在 WeaponXI/cplog 浏览全部代码

或者为了快速查看 query.js 代码是:

var DB = require('../../lib/db.js').DB;

function methods() {
//query object
var Q = {};
//will act as our private variables to workaround asynchronous functions.
//will delete non-required ones when done -- we don't have to, but just for continuity.
exports.privates = {};




//add tag to collection
Q.add = function(tag) {
if (typeof tag === "string") {
//maybe we are adding a tag by name
var obj = {
name: tag
};
} else if (typeof tag === "object" && tag.name) {
//maybe the tag object was specified, and tag's name was provided
var obj = tag;
}

require('mongodb').connect(DB.mongo_url, function(err, db) {
db.collection('tags', function(err, coll) {
coll.insert(obj, {
safe: true
}, function(err, result) {
console.log(result);

});

});
});
}
var callback = {
_set: function(key, val) {
exports.privates[key] = val;
//console.log(JSON.stringify(privates));
},
_get: function(key) {
console.log("get: "+exports.privates.key);
console.log(JSON.stringify(exports.privates));
return exports.privates[key];
},
_unset: function(key) {
delete privates[key];
}
}
var if_exists = function(query, where, callback) {

require('mongodb').connect(DB.mongo_url, function(err, db) {
db.collection(where, function(err, coll) {
coll.findOne(query, function(e, r) {
//console.log(r);
if (r === null) {
callback._set("does_exist", false);
} else {
callback._set("does_exist", true);
}

});
});
});

var result = callback._get("does_exist");
// delete privates.does_exist;

return result;
}

Q.if_exists = function(query, where) {
if_exists(query, where, callback);

}



return Q;
}

var query = exports.query = methods();

function unit_test_add() {
console.log("add query test");
query.add("os");
query.add({
name: "rhcp"
});
}

function unit_test_if_exists() {
console.log("exists tests:");
console.log("should be true: " + query.if_exists({
name: "os"
}, "tags"));
console.log("should be false:" + query.if_exists({
name: "ossuruk"
}, "tags"));

}

function unit_tests() {
console.log("running unit tests...");
unit_test_add();
unit_test_if_exists();

}
unit_tests();

解决方案:

Query.js Query.test.js Gists

谢谢 JohnnyHK!

最佳答案

不能使用异步结果作为函数的返回值。就这么简单。您必须通过作为参数提供给函数的回调将异步结果传递给调用者(或者使用 futures/promises 并有效地推迟该步骤,但这更复杂)。

if_exists 应该看起来像这样:

var if_exists = function(query, where, callback) {

require('mongodb').connect(DB.mongo_url, function(err, db) {
db.collection(where, function(err, coll) {
coll.findOne(query, function(e, r) {
//console.log(r);
if (r === null) {
callback(e, false);
} else {
callback(e, true);
}
// You should either close db here or connect during start up
// and leave it open.
db.close();
});
});
});
}

关于javascript - nodejs mongodb驱动的同步函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13888792/

25 4 0
文章推荐: javascript - 使用 jQuery 回退为 Zepto 编写自定义插件
文章推荐: Javascript将对象的当前内容保存在另一个对象中?
文章推荐: Javascript - 函数工作一次,但再也不会
文章推荐: javascript - jquery jcarousel 错误 : Uncaught TypeError: Object # has no method 'jcarousel'