[英]instantiating object returns an error after export default in typescript
我有一个模型目录:
models/
-- index.ts
-- basemodel.ts
-- auth.ts
-- validator.ts
所有模型中只有auth.ts继承了basemodel
型号/index.ts
export { default as BaseModel } from './basemodel.ts';
export { default as Auth } from './auth.ts';
export { default as Validator } from './validator.ts';
型号/basemodel.ts
import { firestore } from 'firebase-admin';
export default abstract class BaseModel {
private _collection: firestore.Firestore;
get collection(): firestore.Firestore {
return this._collection
}
constructor() {
this._collection = firestore().collection(
this.constructor.name.toLowerCase()
)
}
//... more codes
}
型号/认证
import { BaseModel } from '.';
export default new (class Auth extends BaseModel)()
现在我在middleware / input-validator.ts中创建了一个文件
import * as joi from 'joi';
import { Validator } from '../models';
export default (schema) => (req, res, next) => {
const result = new Validator(schema, req.body, //... callback codes removed)
//.. if result true then next() else throw response err
}
现在,当我尝试提供该功能时 ( I am using firebase function with TS
)
它返回一个错误:
Cannot read property 'firestore' of undefined
现在,我尝试删除auth.ts中的新 内容 ,如下所示:
型号/认证
import { BaseModel } from '.';
export default class Auth extends BaseModel {};
并尝试serve
并且成功了;
现在我的问题是,它是怎么发生的?
我试图检查堆栈跟踪,但它指出firestore to be undefined
的firestore to be undefined
的base model
;
这是模块或打字稿或es6中其他内容的陷阱吗(或缺少它)?
这也是我的tsconfig
{
"compilerOptions": {
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2017",
"resolveJsonModule": true
},
"compileOnSave": true,
"include": ["src"],
"exclude": ["node_modules"]
}
编辑 :
我忘记添加身份验证所调用的服务。
服务/身份验证服务
import { Auth } from '../models';
export default class AuthService {
constructor() {
this.model = Auth;
}
}
现在在帖子中使用
import * as AuthService from './services';
app.post("/login", (req, res) => {
const { email, password } = req.body;
new AuthService().login(email, password)
//.. create token and respond with token
})
无效导入了Admin SDK。
import { firestore } from 'firebase-admin';
它一定要是:
import * as admin from 'firebase-admin'
admin.initializeApp();
const firestore = admin.firestore();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.