2012-01-26 56 views
3

我正在使用Cradle從我的Node.js服務器存儲CouchDB中的對象。該對象包含功能....如何在從CouchDB檢索的文檔上調用函數?

function AnObject(a, b){ 
this.a = a; this.b = b; 
this.addparts = function(){return this.a + this.b;};} 

var cradle = require('cradle'); 
var db = new(cradle.Connection)('http://localhost', 5984, {cache: true, raw: false}).database('myDB'); 

var myObject = new AnObject(1, 2); 
console.log("addparts:" + myObject.addparts()); 
db.save('myObjectId', myObject); 

這工作得很好,並存儲文檔,但是當我找回它,我不能再呼籲返回的文檔的功能...

db.get('myObjectId', function(err, myRetrievedObject){ 
console.log("addparts:" + myRetrievedObject.addparts()); 
}); 

(Property不是函數)Error失敗。

node cradle_test 

cradle_test.js:21 
console.log("addparts:" + myRetrievedObject.addparts()); 
             ^
TypeError: Property 'addparts' of object {"_id":"myObjectId","_rev":"2-83535db5101fedfe30a1548fa2641f29","a":1,"b":2,"addparts":"function(){return this.a + this.b;}"} is not a function 
+0

我會很有趣的有一個模塊,可以保存函數引用的分貝,你可以打電話給他們檢索他們。像dnode這樣的事情。 – fent

回答

2

CouchDB存儲JSON。函數不是有效的JSON。函數永遠不會存儲在數據庫中。

我建議你將函數移出原型。

function AnObject(a, b){ 
    this.a = a; this.b = b; 
} 

AnObject.prototype.addparts = function(){ 
    return this.a + this.b; 
}; 

db.get('myObjectId', function(err, myRetrievedObject){ 
    var obj = Object.create(AnObject.prototype); 
    // for some value of extend (https://github.com/Raynos/pd#pd.extend) 
    extend(obj, myRetrievedObject); 
    console.log("addparts:" + obj.addparts()); 
}); 

這種方式你不保存的功能,你仍然可以使用你的方法操作你的對象。您只需確保將您檢索到的對象作爲AnObject的實例

+0

謝謝雷,這是答案,不幸的是我無法讓你的模塊工作,所以我最終下載和./要求這個文件,而不是'擴展'功能... https://raw.github.com /DracoBlue/spludo/master/core/util.js –

+0

@MichaelDausmann你能突出它的哪部分不起作用嗎?如果您打開pd的錯誤報告,我會解決它。 – Raynos

0

有一種方法可以將函數存儲在CouchDB中:作爲附件

在單獨的.js文件中定義您的函數 (例如,您希望在多個服務器或應用程序實例中共享的一組函數)。

/modules/db.js:

var db = {} 

db.insert = function(nanoDb, object, cb){ 
    //insert new doc 
    nanoDb.insert(object, function(err, body, header) { 
    if (!err) { 
     cb.call(null, {success: true, data: body}); 

    }else{ 
     console.log('[insert] ', err.message); 
     cb.call(null, {success: false, error: err.message}); 
    } 
    }); 
} 

db.bulkInsert = function(nanoDb, array, cb){ 
    //structure for bulk insert 
    var data = { 
    docs: array 
    } 
    //insert new doc 
    nanoDb.bulk(data, function(err, body, header) { 
    if (!err) { 
     cb.call(null, {success: true, data: body}); 

    }else{ 
     console.log('[bulkInsert] ', err.message); 
     cb.call(null, {success: false, error: err.message}); 
    } 
    }); 
} 

db.bulkDelete = function(nanoDb, array, cb){ 
    for(i in array){ 
    array[i]._deleted = true; 
    } 
    var data = { 
    docs: array 
    } 
    //check if the url exists in the db 
    nanoDb.bulk(data, function(err, body) { 
    if (!err){ 
     cb.call(null, {success: true, data: data}); 
    }else{ 
     console.log('[bulkDelete] ', err.message); 
     cb.call(null, {success: false, error: err.message}); 
    } 
    }); 
} 

db.view = function(nanoDb, design, view, params, cb){ 
    nanoDb.view(design, view, params, function(err, body) { 
    if (!err){ 
     var docs = util.extractDocs(body); 
     cb.call(null, {success: true, data: docs}); 
    }else{ 
     console.log('[view] ', err.message); 
     cb.call(null, {success: false, error: err.message}); 
    } 
    }); 
} 

db.search = function(nanoDb, design, index, params, cb){ 
    nanoDb.search(design, index, params, function(err, body) { 
    if (!err) { 
     var docs = util.extractDocsSearch(body); 
     cb.call(null, {success: true, data: docs}); 
    }else{ 
     console.log('[search] ', err.message); 
     cb.call(null, {success: false, error: err.message}); 
    } 
    }); 
} 

db.follow = function(nanoDb, params){ 
    var feed = nanoDb.follow(params); 
    return feed; 
} 

module.exports = db; 

使用CouchApp部署的功能附件(在設計文檔):

//your couchapp 
var couchapp = require('couchapp') 

//url to your database 
var url = '...'; 

//empty design doc (for attachments) 
ddoc = { 
    _id: '_design/mods' 
}; 

//folder containing .js files 
couchapp.loadAttachments(ddoc, './modules/'); 

//this function uploads your attachments 
couchapp.createApp(ddoc, url, function(app) { 
    app.push(function(){ 
    //do something 
    }); 
}); 

現在,得到了功能,無論你需要他們:

//use mikaels request module if you like 
var request = require('request'); 

//tell the app where to get your .js file 
//sometimes a good idea to persist these references in memory or even in your couchdb 
var fileUrl = '/_design/modules/db.js' 

//set this variable in the proper scope 
var db; 

//we'll use this to 'require' the .js file 
var _require = function(src, file) { 
    var m = new module.constructor(); 
    m.paths = module.paths; 
    m._compile(src, file); 
    return m.exports; 
} 

request({ url: fileUrl, json: true }, function (err, response, data) { 
    if (!err && response.statusCode === 200) { 
    //now we assign our required .js file (object w/functions) back into usable form 
    //woot woot! 
    db = _require(data); 
    }else{ 
    console.log('[request]', err); 
    } 
}); 

做東西!

db.doSomething()