无法访问Express.js中的用户对象字段

时间:2015-02-02 00:29:15

标签: javascript node.js mongodb express

我使用护照进行身份验证,使用MongoDB进行存储。登录并尝试访问req.user对象后出现问题。将它传递给视图,我可以看到用户具有值[object Object],而其中的任何字段都是未定义的。

路线:

app.get('/upload', isLoggedIn, function(req, res) {
    res.render('upload', {
        user : req.user
    });
});

查看:

<h1>The File Upload Page</h1>

<p>
    <%= user %>
    <%= user.name %>
    <%= user._id %>
</p>

Output from view

有什么想法吗?

编辑:

app.post('/upload', isLoggedIn, function(req, res) {
console.log(JSON.stringify(util.inspect(req.user)));

MongoClient.connect(url, function(err, db) {
    db.collection('users', function (err, usersCollection) {
        usersCollection.update( { _id : req.user._id }, {$push : { images : 'testVal'} }, function (err) {
            if (err) {
                console.log('Error adding image to user image array');
            }
            db.close();
            res.send('all done');
        });
    });
});
});

req.user输出:

{ db:
   { domain: null,
     _events: {},
     _maxListeners: 10,
     databaseName: 'meninas',
     serverConfig:
      { domain: null,
        _events: {},
        _maxListeners: 10,
        auth: [Getter],
        _callBackStore: [Object],
        _commandsStore: [Object],
        _dbStore: [Object],
        host: 'localhost',
        port: 27017,
        options: [Object],
        internalMaster: true,
        connected: true,
        poolSize: 5,
        disableDriverBSONSizeCheck: false,
        _used: true,
        replicasetInstance: null,
        emitOpen: false,
        ssl: false,
        sslValidate: false,
        sslCA: null,
        sslCert: undefined,
        sslKey: undefined,
        sslPass: undefined,
        serverCapabilities: [Object],
        name: 'localhost:27017',
        socketOptions: [Object],
        logger: [Object],
        eventHandlers: [Object],
        _serverState: 'destroyed',
        _state: [Object],
        recordQueryStats: false,
        socketTimeoutMS: [Getter/Setter],
        _readPreference: [Object],
        db: [Circular],
        dbInstances: [Object],
        connectionPool: [Object],
        isMasterDoc: [Object] },
     options:
      { read_preference_tags: null,
        read_preference: 'primary',
        url: 'mongodb://localhost/meninas',
        native_parser: true,
        readPreference: [Object],
        safe: false,
        w: 1 },
     _applicationClosed: false,
     slaveOk: false,
     bufferMaxEntries: -1,
     native_parser: true,
     bsonLib:
      { BSON: [Object],
        Long: [Object],
        ObjectID: [Object],
        DBRef: [Object],
        Code: [Object],
        Timestamp: [Object],
        Binary: [Object],
        Double: [Object],
        MaxKey: [Object],
        MinKey: [Object],
        Symbol: [Object] },
     bson: { promoteLongs: true },
     bson_deserializer:
      { Code: [Object],
        Symbol: [Object],
        BSON: [Object],
        DBRef: [Object],
        Binary: [Object],
        ObjectID: [Object],
        Long: [Object],
        Timestamp: [Object],
        Double: [Object],
        MinKey: [Object],
        MaxKey: [Object],
        promoteLongs: true },
     bson_serializer:
      { Code: [Object],
        Symbol: [Object],
        BSON: [Object],
        DBRef: [Object],
        Binary: [Object],
        ObjectID: [Object],
        Long: [Object],
        Timestamp: [Object],
        Double: [Object],
        MinKey: [Object],
        MaxKey: [Object],
        promoteLongs: true },
     _state: 'connected',
     pkFactory:
      { [Function: ObjectID]
        index: 15495053,
        createPk: [Function: createPk],
        createFromTime: [Function: createFromTime],
        createFromHexString: [Function: createFromHexString],
        isValid: [Function: isValid],
        ObjectID: [Circular],
        ObjectId: [Circular] },
     forceServerObjectId: false,
     safe: false,
     notReplied: {},
     isInitializing: true,
     openCalled: false,
     commands: [],
     logger: { error: [Function], log: [Function], debug: [Function] },
     tag: 1422844480369,
     eventHandlers:
      { error: [],
        parseError: [],
        poolReady: [],
        message: [],
        close: [] },
     serializeFunctions: false,
     raw: false,
     recordQueryStats: false,
     retryMiliSeconds: 1000,
     numberOfRetries: 60,
     readPreference: { _type: 'ReadPreference', mode: 'primary', tags: undefined } },
  collection:
   { db:
      { domain: null,
        _events: {},
        _maxListeners: 10,
        databaseName: 'meninas',
        serverConfig: [Object],
        options: [Object],
        _applicationClosed: false,
        slaveOk: false,
        bufferMaxEntries: -1,
        native_parser: true,
        bsonLib: [Object],
        bson: [Object],
        bson_deserializer: [Object],
        bson_serializer: [Object],
        _state: 'connected',
        pkFactory: [Object],
        forceServerObjectId: false,
        safe: false,
        notReplied: {},
        isInitializing: true,
        openCalled: false,
        commands: [],
        logger: [Object],
        tag: 1422844480369,
        eventHandlers: [Object],
        serializeFunctions: false,
        raw: false,
        recordQueryStats: false,
        retryMiliSeconds: 1000,
        numberOfRetries: 60,
        readPreference: [Object] },
     collectionName: 'users',
     internalHint: null,
     opts: {},
     slaveOk: false,
     serializeFunctions: false,
     raw: false,
     readPreference: { _type: 'ReadPreference', mode: 'primary', tags: undefined },
     pkFactory:
      { [Function: ObjectID]
        index: 15495053,
        createPk: [Function: createPk],
        createFromTime: [Function: createFromTime],
        createFromHexString: [Function: createFromHexString],
        isValid: [Function: isValid],
        ObjectID: [Circular],
        ObjectId: [Circular] },
     serverCapabilities: undefined },
  selector: { _id: '54cebe00799372571d6bcb70' },
  fields: undefined,
  skipValue: 0,
  limitValue: 0,
  sortValue: undefined,
  hint: null,
  explainValue: undefined,
  snapshot: undefined,
  timeout: true,
  tailable: undefined,
  awaitdata: undefined,
  oplogReplay: undefined,
  numberOfRetries: 5,
  currentNumberOfRetries: 5,
  batchSizeValue: 0,
  raw: false,
  readPreference: { _type: 'ReadPreference', mode: 'primary', tags: undefined },
  returnKey: undefined,
  maxScan: undefined,
  min: undefined,
  max: undefined,
  showDiskLoc: undefined,
  comment: undefined,
  tailableRetryInterval: 100,
  exhaust: false,
  partial: false,
  slaveOk: false,
  maxTimeMSValue: undefined,
  connection: undefined,
  totalNumberOfRecords: 0,
  items: [],
  cursorId: { _bsontype: 'Long', low_: 0, high_: 0 },
  dbName: undefined,
  state: 0,
  queryRun: false,
  getMoreTimer: false,
  collectionName: 'meninas.users' }
POST /upload 200 2181.101 ms - 8

0 个答案:

没有答案