0

高速アプリの app.js ファイルで、req.params.company で未定義になっています。関連するコードは次のとおりです...

var mongoose = require( 'mongoose' );
var authenticate = express.basicAuth(function(user, pass, req, res, next, callback) {
    Auth = mongoose.model('Auth');
    Auth.findOne({ token: user }, function(err, authObject){
            if (err) return next(err);
        //The next line is where the problem lies.  Req.params.company is undefined.    
            var result = (user === authObject.token && req.params.company === authObject.companyName);
            console.log('result = ' +result);
            return result;
            //callback(null /* error */, result);
            })
});



app.get('/api/:company', authenticate, api.company);

質問とは関係ないと思いますが、念のため、私の auth.js モデルのコードを示します。

var mongoose = require( 'mongoose' );

function toLower (v) {
  return v.toLowerCase();
}

var authSchema = new mongoose.Schema({
    token : { type : String, required : true, unique : true, index: true, set: toLower },
    companyName : { type : String, set: toLower },
    brandName : { type : String, set: toLower },
    active : Boolean
});

var Auth = mongoose.model('Auth', authSchema);

最後に、このコードの目的は、http 基本認証 (パスワードが null) でユーザー変数として渡されるトークンを認証することだけです。また、ルート パラメーターとして指定された会社名のトークンを承認しようとします。

req.params.company が undefined として戻ってくる理由を誰かが理解するのを手伝ってくれますか? 「req」を単独でログに記録すると、「[Function]」が返されます。

4

1 に答える 1

0

間違った場所でコードを探しているようです。おそらくconsole.log(引数)を実行してください。また、役立つコードへのリンクは次のとおりです: http://blog.modulus.io/nodejs-and-express-basic-authentication

于 2013-09-08T00:26:33.937 に答える