创build我自己的模型与节点谁允许新的模型和Model.find()

我试图创build一个模型与节点,我想能够像这样使用它:

要求模型

var Example = require('./models/example'); 

创build新的对象

该模型可以使用new创build一个新的对象

 var example = new Example({ data:'example' }); 

该模型可以find对象的使用方法

 Example.find({ data: 'mongoQuery' }, function(err, examples) {}); 

保存

该模型可以帮助find对象的使用方法,用方法返回对象。

 Example.findOne({ query: 'example' }, function(err, example) { example.set({ data: 'another data' }); example.save(); }); 

使用示例

我希望能够使用这样的模型创build,查找(一个或多个),增加和删除一个标记(例如)。 我将在控制器或lib上使用它。

 var Token = require('./models/token); // Create and save a new token var new_token = new Token({ key: 'abcdef' }); new_token.save(); // find multiple tokens, update, and save var token = Token.find({key: 'abcderf'}, function(tokens) { for(var i in tokens) { tokens[i].set({key: '123456'}); tokens[i].save(); } }); 

我已经尝试了很多东西,但是我不能创build一个同时允许new ExampleExample.find()的模块。

主干:

我尝试使用Backbone,新的Token({/ * attributes * /})工作,但find函数返回一个错误: TypeError: Object function (){ return parent.apply(this, arguments); } has no method 'find' TypeError: Object function (){ return parent.apply(this, arguments); } has no method 'find'

 var Token = Backbone.Model.extend({ initialize: function(attributes) { console.log("Token create"); console.log(" attributes : ", attributes); }, find : function(query, callback) { console.log("Token find"); console.log(" query : ", query); callback(null, [ /* tokens */]); } }); module.exports = Token; 

目的

当我试图使用一个对象,查找function工作正常,但我不能用它与new返回错误: TypeError: object is not a function

 module.exports = { find : function(query, callback) { console.log("[Step] models/token.js - find"); console.log(" query : ", query); callback(null, []); } }; 

创build一个模型来处理控制器上的所有对象的最好方法是什么?

感谢您的帮助!

干得好 :

 YourModule = function(data){ this.data = data; // this.data is now a property of the object you create with New. }; YourModule.prototype.find = function(query, callback) { console.log("[Step] models/token.js - find"); console.log(" query : ", query); callback(null, []); return this.data; // this.data is available here. }; YourModule.prototype.save = function(data) { this.data = data; // this.data is available here to. }; module.exports = YourModule; 

你不能使用new关键字来实例化一个对象,但是你可以做Object.create来做到这一点。 我喜欢我的例子。

所以…我在@Neta Meta回复中创build了一个模型基础:

./models/token.js

 var Backbone = require('backbone'); var db = require('../lib/database'); var Token = Backbone.Model.extend({ initialize: function(attributes) { }, save: function(callback) { db.collection('tokens').insert(self.attributes, function(err) { if (typeof callback === 'function') { callback(err, self); } }); } }); module.exports.create = function(attr, callback) { callback(null, new Token(attr)); }; module.exports.findOne = function(query, callback) { db.collection('tokens').findOne(query, function(err, result) { callback(err, new Token(result)); }); }; 

所以现在我可以这样使用它:

./controllers/example.js

 var Token = require('../models/token'); // Create one : Token.create({key: 'new data'}, function(err, token) { token.set({key: 'updated data'}); token.save(); }); // Find one : Token.findOne({key: 'updated data'}, function(err, token) { token.set({key: 're-updated data'}); token.save(function(err) { console.log("Token saved"); }); });