2014-10-20 51 views
10

這是令人生氣的,我如何獲得一個迴環模型,以便可以通過編程方式使用它?我有一個名爲「通知」的Persisted模型。我可以使用REST瀏覽器與它進行交互。我希望能夠在服務器中使用它,即Notification.find(...)。我執行app.models()並可以看到它列出。我已經這樣做了:如何獲取Strongloop回送模型?

var Notification = app.models.Notification; 

並得到一個大胖子「未定義」。我這樣做了:

var Notification = loopback.Notification; 
app.model(Notification); 
var Notification = app.models.Notification; 

和另一個大胖子「未定義」。

請解釋我必須做的就是我一直在使用定義的模型的保持:

slc loopback:model 

在此先感謝

+0

查看https://groups.google.com/forum/#!topic/loopbackjs/Z5VNL5Aw4Cs – 2014-10-21 18:43:33

+0

可能對某人有用:如果您在「初始化」之前訪問模型,則會給出未定義的模型。只要嘗試從放置在server/boot中的腳本訪問模型。很好的例子在這裏:http://docs.strongloop.com/display/public/LB/Defining+boot+scripts#Definingbootscripts-Synchronousbootscripts – IvanZh 2014-12-26 21:10:43

回答

8

您可以使用ModelCtor.app.models.OtherModelName從您自定義的方法訪問其他車型。

/** common/models/product.js **/ 
module.exports = function(Product) { 
    Product.createRandomName = function(cb) { 
    var Randomizer = Product.app.models.Randomizer; 
    Randomizer.createName(cb); 
    } 

    // this will not work as `Product.app` is not set yet 
    var Randomizer = Product.app.models.Randomizer; 
} 

/** common/models/randomizer.js **/ 
module.exports = function(Randomizer) { 
    Randomizer.createName = function(cb) { 
    process.nextTick(function() { 
     cb(null, 'random name'); 
    }); 
    }; 
} 

/** server/model-config.js **/ 
{ 
    "Product": { 
    "dataSource": "db" 
    }, 
    "Randomizer": { 
    "dataSource": null 
    } 
} 
+0

如果你的代碼中沒有另外的模型構造函數但是你有一個實例該模型? – JBCP 2015-12-12 04:29:05

+1

@JBCP你可以通過'constructor'屬性(通常)獲得構造函數。例如。 「productInstance.constructor.app.models.Randomizer」。 – 2015-12-14 08:57:52

+0

謝謝。如果我有一個模型實例,是否可以安全地使用app.models.OtherModel,因爲我知道現在應用程序已經初始化了?這是我迄今爲止所做的。 – JBCP 2015-12-14 12:27:13

0

我知道這篇文章在這裏很久以前。但因爲我得到了同樣的問題,最近幾天,這裏就是我想出了最新的迴環API:

你可以得到,你的模型連接如下應用:

ModelX.js

module.exports = function(ModelX) { 
 
    //Example of disable the parent 'find' REST api, and creat a remote method called 'findA' 
 
\t var isStatic = true; 
 
\t ModelX.disableRemoteMethod('find', isStatic); 
 

 
\t ModelX.findA = function (filter, cb) { 
 
     
 
     //Get the Application object which the model attached to, and we do what ever we want 
 
\t ModelX.getApp(function(err, app){ 
 
\t if(err) throw err; 
 
\t //App object returned in the callback 
 
\t app.models.OtherModel.OtherMethod({}, function(){ 
 
\t if(err) throw err; 
 
\t //Do whatever you what with the OtherModel.OtherMethod 
 
     //This give you the ability to access OtherModel within ModelX. 
 
     //... 
 
     }); 
 
    }); 
 
\t } 
 
    
 
    //Expose the remote method with settings. 
 
\t ModelX.remoteMethod(
 
\t 'findA', 
 
\t { 
 
\t \t description: ["Remote method instaed of parent method from the PersistedModel", 
 
\t \t \t "Can help you to impliment your own business logic"], 
 
\t \t http:{path: '/finda', verb: 'get'}, 
 
\t \t accepts: {arg:'filter', 
 
\t \t type:'object', 
 
\t \t description: 'Filter defining fields, where, include, order, offset, and limit', 
 
\t \t http:{source:'query'}}, 
 
\t \t \t returns: {type:'array', root:true} 
 
\t \t } 
 
\t); 
 
};

貌似我不是這裏的代碼塊格式做得很好......

你也應該小心的時機,當這個「getApp」被調用,這很重要,因爲如果在初始化模型時很早調用此方法,則會發生類似「未定義」錯誤的情況。