2017-05-06 34 views
0

我做了一個博客,用戶只能編輯他們創建的博客。但現在我想做一個管理員,可以編輯和刪除他想要的任何職位。javascript中有equals()的用戶授權

if(foundBlog.author.id.equals(
    req.user._id || 
    foundBlog.author.username === "ADMIN" 
)) { 
    next(); 
} else { 
    res.redirect("back"); 
} 

但我的代碼不能正常工作。

+0

*我的代碼無法使用*如何?怎麼了? –

回答

0

equals()不是一個有效的String函數,除非您自己實現了此功能。如果你還沒有和希望,你可以做的

String.prototype.equals = function(str){ 
    return this.toString() === str; 
} 

簡單的東西,然後

"hello".equals("hello") // gives true 
"hello".equals("kitty") // gives false 

不過,我會建議對中毒的一個衆所周知的原型/內置對象或任何共享的全球空間導致這是災難的祕訣。

就這樣說,我只是去嚴格的平等比較。

if(foundBlog.author.id === req.user._id || foundBlog.author.username === "ADMIN") 

不傷害沒有犯規。