console.log(self)有输出字段但是console.log(self.output)未定义

时间:2017-01-05 18:56:06

标签: typescript

我遇到这个奇怪的问题。当我console.log(self)变量时,它具有输出键集并且具有其中的所有值。但是,如果我尝试console.log(self.output)它给了我未定义,有谁知道它为什么会发生?

这是我的代码:

interface SearchOutput {
    total: 0,
    per_page: number,
    current_page: number,
    last_page: number,
    from: number,
    to: number,
    data: Array<Object>
}

interface SearchParams {
    query: string,
    perPage: number,
    index: string,
    type: string,
    page: number
}

export class Elastic {
    from: number;
    to: number;
    client: any;
    output: SearchOutput;
    error: any;

    constructor(host: string, log: string, elasticsearch: any) {
        this.client = new elasticsearch.Client({
            host: host,
            log: log
        })
    }

    public search({query, perPage, index, type, page}:SearchParams): SearchOutput {
        let self = this;
        this.client.search({
                'index': index,
                'type': type,
                'body': {
                    'query': {
                        'bool': {
                            'must': {
                                'query_string': {
                                    'query': query,
                                    'default_operator': 'AND',
                                    'fuzziness': 1
                                }
                            }, 'filter': []
                        }
                    }
                },
                'size': perPage,
                'from': 0
            },
            (err, response) => {
                const {hits, hits: {total}} = response
                const lastPage = Math.ceil(total / perPage)
                const from = (page * perPage) - perPage + 1
                const to = (page < lastPage) ? page * perPage : total
                let output = {
                    total: total,
                    per_page: perPage,
                    current_page: page,
                    last_page: lastPage,
                    from: from,
                    to: to,
                    data: []
                }
                if (total >= 1) {
                    for (const key in hits.hits) {
                        if (hits.hits.hasOwnProperty(key)) {
                            output.data.push(hits.hits[key]._source);
                        }
                    }
                } else {
                    output.data = [];
                }
                self.output = output;
            }
        );
        console.log(self); // outputs Elastic {client: EsApiClient, output:SearchOutput Objest with all values}
        console.log(self.output) // outputs undefined
        return self.output;
    }
}

1 个答案:

答案 0 :(得分:0)

console.log移动到回调中,如下所示:

    this.client.search({
            'index': index,
            'type': type,
            'body': {
                'query': {
                    'bool': {
                        'must': {
                            'query_string': {
                                'query': query,
                                'default_operator': 'AND',
                                'fuzziness': 1
                            }
                        }, 'filter': []
                    }
                }
            },
            'size': perPage,
            'from': 0
        },
        (err, response) => {
            const {hits, hits: {total}} = response
            const lastPage = Math.ceil(total / perPage)
            const from = (page * perPage) - perPage + 1
            const to = (page < lastPage) ? page * perPage : total
            let output = {
                total: total,
                per_page: perPage,
                current_page: page,
                last_page: lastPage,
                from: from,
                to: to,
                data: []
            }
            if (total >= 1) {
                for (const key in hits.hits) {
                    if (hits.hits.hasOwnProperty(key)) {
                        output.data.push(hits.hits[key]._source);
                    }
                }
            } else {
                output.data = [];
            }
            self.output = output;
            console.log(self.output);
        }
    );

编辑:这是处理返回并返回的异步数据的一种方法。

您当前的搜索方式签名:public search({query, perPage, index, type, page}:SearchParams): SearchOutput

将其更新为public search({query, perPage, index, type, page}:SearchParams, callback): SearchOutput

现在,当您获得数据时(在此方法的回调中),您可以调用传入的回调:

if (total >= 1) {
    for (const key in hits.hits) {
        if (hits.hits.hasOwnProperty(key)) {
            output.data.push(hits.hits[key]._source);
        }
    }
} else {
    output.data = [];
}
    callback(output) // calls callback method passed in with the returned value

现在你可以这样调用搜索:

search(
    {'somequery', 'someperPage', 'someindex', 'sometype', 'somepage'}, 
    (data) => console.log(data)
)