2017-01-27 34 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); 
    } 
    }); 
};