2016-08-18 5 views
1

に存在するかどうかを確認(https://stormpath.com/blog/build-nodejs-express-stormpath-appstormpath、発現 - ユーザは、この例での作業経路

Iは経路および任意のユーザの一部がプロファイルアカウント表示するビューを追加しました。正常に動作します

username: jsmith

http://localhost:3000/-jsmith (note the -) 

- 。何のユーザーがログインしていない場合であっても

ユーザーが存在しない場合、アプリはちょうどハングし、何も返されません。

/-jsmithxxx 

Q:私は、ユーザーが存在するかどうかをテスト、および、 "ユーザーが見つかりません" という表示に戻るにはどうすればよい

おかげで、ロブ

app.get('/-:id', function (req, res, next) { 
    console.log('the response will be sent by the next function ...'); 
    var id = req.params.id; 
    console.log(id); 
    next(); 
}, 
function (req, res) { 
    var id = req.params.id; 
req.app.get('stormpathApplication').getAccounts({ username: id }, (err, accounts) => { 
    if (err) throw err; 
    accounts.each((account, cb) => { 
    console.log('Found matching account:', account); 
    cb(); 
    console.log('username:' + account.username) 
    res.render('user', { 
    email: account.email, 
    surname: account.surname, 
    account:account // this passes object, which can be used in view, no need to define email:account.email in server.js 

    }); 
}); 
}); 
} 
); 

答えて

0

これはあなたが望むものであるように見えます:

app.get('/-:id', function(req, res, next) { 
    var id = req.params.id; 
    var spApp = req.app.get('stormpathApplication'); 
    var acc; 

    spApp.getAccounts({ username: id }, function(err, accounts) { 
    if (err) { 
     return res.send('An error occured. Please try again.'); 
    } 

    accounts.each(function(account, cb) { 
     acc = account; 
     cb(); 
    }, function() { 
     if (!acc) { 
     return res.send('User not found.'); 
     } 

     return res.render('user', { 
     email: account.email, 
     surname: account.surname, 
     account:account 
     }); 
    }); 
    }); 
}); 
関連する問題