我不明白为什么 fetch 函数没有返回我想要的信息:
(function(){
var root = this;
var Database = root.Database = {};
var Db = require('mongodb').Db,
Connection = require('mongodb').Connection,
Server = require('mongodb').Server;
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : Connection.DEFAULT_PORT;
// Require Underscore, if we're on the server, and it's not already present.
var _ = root._;
if (!_ && (typeof require !== 'undefined')) _ = require('./underscore');
Database.ActiveRecord = function(attributes, collection){
this.collection = collection;
this.cid = _.uniqueId('c');
attributes || (attributes = {});
this.attributes = attributes;
};
// Connecting to database and setting the connection as a shared variable among ActiveRecords
console.log("Connecting to " + host + ":" + port);
Database.ActiveRecord.prototype.db = new Db('node-mongo-eslip', new Server(host, port, {}));
_.extend(Database.ActiveRecord.prototype, {
initialize: function(){},
// Returns `true` if the attribute contains a value that is not null
// or undefined.
has: function(attr) {
return this.attributes[attr] != null;
},
// Sets attributes
set: function(key, value){
var attrs, attr, val;
if(_.isObject(key) || key == null){
throw Error("The key should not be an object or null");
}else{
attrs = {};
attrs[key] = value;
}
if (!attrs) return this;
var now = this.attributes;
for(attr in attrs){
val = attrs[attr];
if(!_.isEqual(now[attr], val)) now[attr] = val;
}
},
unset: function(key){
return this.set(attr, null);
},
toJSON: function() {
return _.clone(this.attributes);
},
fetch: function(query, fields, options){
var record = this;
record.db.open(function(err, db){
if(!(record.collection||(typeof record.collection === 'string'))) throw Error('You should define a name attribute, which represents the collection of the Database');
db.collection(record.collection, function(err, collection){
console.log('Fetching...');
collection.find(query, fields, options).toArray(function(err, docs) {
return docs;
});
});
});
},
save: function(){
var record = this;
record.db.open(function(err, db){
if(!(record.collection||(typeof record.collection === 'string'))) throw Error('You should define a name attribute, which represents the collection of the Database');
db.collection(record.collection, function(err, collection){
collection.insert(_.clone(record.attributes), {safe:true},
function(err, objects) {
if (err) console.warn(err.message);
if (err && err.message.indexOf('E11000 ') !== -1) {
console.log('This id has already been inserted into the database');
}
});
});
console.log('Saved!');
});
}
});
}());
我花了很多时间试图弄清楚缺少什么,但没有成功,也许有人会更有可能弄清楚。
最佳答案
当您尝试从中运行 return docs
的回调函数正在执行时,外部 fetch
函数已经返回——正如@SLaks 所建议的,欢迎来到异步编程的精彩世界。
Promises 是处理异步代码的绝佳方式——它们在 jQuery、Dojo 和其他库和工具包中可用。您的 fetch
方法可以返回一个 promise,调用 fetch
方法的代码可以在返回的 promise 被“解决”时使用react。这是 Dojo 的样子:
fetch: function(query, fields, options){
var record = this, dfd = new dojo.Deferred;
record.db.open(function(err, db){
if(!(record.collection||(typeof record.collection === 'string'))) throw Error('You should define a name attribute, which represents the collection of the Database');
db.collection(record.collection, function(err, collection){
console.log('Fetching...');
collection.find(query, fields, options).toArray(function(err, docs) {
dfd.resolve(docs);
});
});
});
return dfd.promise;
},
然后,调用 fetch 的代码可能如下所示:
myDB.fetch().then(function(docs) {
// do whatever you need with the docs
});
关于javascript - 对象未在 Javascript 中返回所需的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9169811/