无法读取未定义的属性原型

时间:2019-11-25 06:14:27

标签: node.js typescript

我是打字机的新手,经过这么多忙碌=,我终于能够编写一个代码段,该段代码没有抛出任何错误,但不幸的是,它在运行时在说

Cannot read property prototype of undefined. 

这就是我在代码中使用它的方式

import FirebaseSession from 'connect-session-firebase'

app.use(
            session({
                store: new FirebaseSession({
                    database: context.realtimeDB
                }),
                name: '__session', // Must use this name for firebase hosting
                secret: 'kkkkj9h9', // TODO: Move to ENV
                resave: true,
                saveUninitialized: true
            })
        )

在其中创建了我的自定义类型的地方,该类型为/connect-session-firebase/index.ts

import * as admin from 'firebase-admin'

declare class FirebaseSession {
  constructor(options: FirebaseSession.Options)
}


declare namespace FirebaseSession {
  export interface Options {
    database: admin.database.Database | admin.firestore.Firestore, 
    sessions?: string, 
    reapInterva?: string,
    errorIfSessionNotFound?: any
  }
}


export = FirebaseSession

在构建时会创建以下js

const connect_session_firebase_1 = __importDefault(require("connect-session-firebase"));
    this.use(express_session_1.default({
            store: new connect_session_firebase_1.default({
                database: context.realtimeDB
            }),
            name: '__session',
            secret: 'kkkkj9h9',
            resave: true,
            saveUninitialized: true
        }));

如果有人可以帮助我修复问题并告诉我我做错了什么,请问我是否会深爱并感激?

错误

Cannot read property 'prototype' of undefined
    at new connectSessionFirebase (/Users/aa/Desktop/bltypescript/functions/node_modules/connect-session-firebase/lib/connect-session-firebase.js:88:49)
    at new BaseConfig (/Users/aa/Desktop/bltypescript/functions/lib/BaseConfig.js:32:20)
    at new ApiStatus (/Users/aa/Desktop/bltypescript/functions/lib/api/status.f.js:6:9)
    at Object.<anonymous> (/Users/aa/Desktop/bltypescript/functions/lib/index.js:44:25)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)

1 个答案:

答案 0 :(得分:0)

这是一个运行时错误,与TypeScript无关。

查看官方文档usage code example

const session = require('express-session');
const FirebaseStore = require('connect-session-firebase')(session);
// ...

该文档明确指示您将session实例传递给函数,以便获得FirebaseStore类对象。但是您没有按照指示进行操作。

在TypeScript中的代码上方:

import * as session from 'express-session';
import * as connectSessionFirebase from 'connect-session-firebase';
const FirebaseStore = connectSessionFirebase(session);
// ...