TypeError:无法读取未定义的属性“ myDate”

时间:2019-01-31 23:56:40

标签: node.js typescript express mongoose

我正在尝试编写一个相当基本的代码,但收到浏览器错误。 expressjs日志也显示错误。

  

TypeError:无法读取未定义的属性“ myDate”       在getReportTable(XXX \ dist \ controllers \ crm \ uploadHealthTable.js:18:70)       在Layer.handle [作为handle_request](XXX \ node_modules \ express \ lib \ router \ layer.js:95:5)       在下一个(XXX \ node_modules \ express \ lib \ router \ route.js:137:13)

import { Request, Response } from "express";
import * as mongoose from "mongoose";
import { uploadHealthTableSchema } from "../../models/crm/uploadHealthTableModel";

const reportTable = mongoose.model("USER_UPLOAD", uploadHealthTableSchema);

export class UploadHealthTable {

private myDate = new Date() ;

    public getReportTable(req: Request, res: Response) {
    reportTable.find({companyId: "18", uploadTime: { $gte: this.myDate.setDate(this.myDate.getDate() - 5) } },
    "_id fileName status uploadTime", { sort: {uploadTime: -1} }, (err, report) => {
        if (err) {
        res.send(err);
        }
        res.json(report);
    });
    }
}

调用此功能-

export class CRMRoutes {
    public uploadHealthTable: UploadHealthTable = new UploadHealthTable() ;

    public routes(app: any): void {

        app.route("/support/reporttable/1/all")
        .get(this.uploadHealthTable.getReportTable) ;
    }
}

1 个答案:

答案 0 :(得分:0)

您需要将此函数转换为箭头函数

 self.functions.httpsCallable("getText").call(["public_token" : self.userMap["plaidPublicToken"]]) { (result, error) in
                    if let error = error as NSError? {
                        if error.domain == FunctionsErrorDomain {
                            let code = FunctionsErrorCode(rawValue: error.code)
                            let message = error.localizedDescription
                            let details = error.userInfo[FunctionsErrorDetailsKey]
                            print(message)
                        }
                        // ...
                    }
                    if let text = (result?.data as? [String: Any])?["text"] as? String {
                        print (text)
                    }
                }

说明: 因为您将函数作为参数传递,所以request.onload = function() { var data = JSON.parse(this.response); data.results.forEach(film=> { ... var baseImgURL = "https://image.tmdb.org/t/p/w92" + film.poster_path; var filmImage = document.createElement("img"); filmImage.src = baseImgURL; if(filmImage.src.indexOf("null")){ filmImage.src = "/img/imagenotavailable.png"; } 的值仍在先前的函数上下文中

有用的链接: