2017-01-27 38 views
1

_all_遠程方法我用回送3 我在我的模型的JS代碼此行(survey.js):隱藏在環回模式

let enabledRemoteMethods = [] 

Survey.sharedClass.methods().forEach(function(method) { 
    console.log(method.name, method.isStatic) 
    let matchingEnabledRemoteMethod = _.find(enabledRemoteMethods, {name: method.name}); 

    if (!matchingEnabledRemoteMethod) { 
    Survey.disableRemoteMethodByName(method.name, method.isStatic); 
    } 
}); 

工程....差不多。我仍然可以在資源管理器中看到「PATCH/surveys/{id}」的REST端點。我的期望是:在瀏覽器中不應該列出任何REST端點。

然後我檢查對應於該操作的URL,它是:

http://localhost:3000/explorer/#!/Survey/Survey_prototype_patchAttributes

enter image description here

其中,根據文檔,意味着patchAttributes是一個靜態方法。

然後我與控制檯中的輸出進行交叉檢查...它說:pathAttributes是而不是靜態。

Incosistency!

enter image description here

我甚至曾嘗試加入這一行:

Survey.disableRemoteMethodByName("patchAttributes", true); 

而且

Survey.disableRemoteMethodByName("patchAttributes", false); 

沒有運氣。

有人可以確認它是否是環回3中的錯誤(我不知道環回2,沒有檢查)?如果這是一個錯誤,我不必花時間在它上面,只是等到它被修復。但是,如果它不是一個bug,有人能指出我的代碼中缺少什麼嗎?

謝謝!


更新:想出如何

這一行我能擺脫它:

Survey.disableRemoteMethodByName("prototype.patchAttributes", true); 

第二個參數似乎並不重要(你可以把假的以及)。不知道爲什麼(我想它應該只接受真實的)。

這是我目前的解決方案:

let disabledPrototypesRemoteMethods = ['patchAttributes'] 
let enabledRemoteMethods = [ 
    "create", "findById", "replaceById", "deleteById", 
    "replaceOrCreateQuestion" 
] 
Survey.sharedClass.methods().forEach(function(method) { 
    if (enabledRemoteMethods.indexOf(method.name) == -1) { 
    Survey.disableRemoteMethodByName(method.name); 
    } 

    if (disabledPrototypesRemoteMethods.indexOf(method.name) > -1) { 
    Survey.disableRemoteMethodByName("prototype." + method.name); 
    } 
}); 

儘管如此,一個小細節:這件事情仍然顯示了(我想它提供了POST替代了對replaceById運行正常PUT ...,但我不想要;我想強迫我的API的用戶去只用PUT):

http://localhost:3000/explorer/#!/Survey/Survey_replaceById_post_surveys_id_replace

enter image description here

我試圖加入這一行:

Survey.disableRemoteMethodByName("replaceById_post_surveys_id_replace"); 

沒有運氣。

無論如何...希望這對他人有用; loopback doc是一種粗略的。


回答

0

您也可以通過查看stringName屬性來獲取原型方法。這樣你可以將原型包含在你的列表中。

stringName在值中包含sharedClass名稱,因此您需要解析該名稱。

module.exports = BusinessProfileContacted => { 
    const enabledRemoteMethods = ["create", "findById", "replaceById", "deleteById", "replaceOrCreateQuestion", "prototype.replaceAttributes"]; 

    Survey.sharedClass.methods().forEach(method => { 
    const methodName = method.stringName.replace(/.*?(?=\.)/, '').substr(1); 
    if (enabledRemoteMethods.indexOf(methodName) === -1) { 
     Survey.disableRemoteMethodByName(methodName); 
    } 
    }); 
};