Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript console.log(self)有输出字段,但console.log(self.output)未定义_Typescript - Fatal编程技术网

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

Typescript console.log(self)有输出字段,但console.log(self.output)未定义,typescript,Typescript,我有一个奇怪的问题。当Iconsole.log(self)变量时,它设置了输出键并包含所有值。但是如果我尝试console.log(self.output)它会给我未定义的信息,有人知道为什么会发生这种情况吗 这是我的密码: interface SearchOutput { total: 0, per_page: number, current_page: number, last_page: number, from: number, to: n

我有一个奇怪的问题。当I
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;
    }
}
接口搜索输出{
总数:0,
每页:编号,
当前页面:编号,
最后一页:编号,
发件人:号码:,
收件人:号码:,
数据:数组
}
接口搜索参数{
查询:字符串,
佩奇:号码,
索引:string,
类型:字符串,
页码:编号
}
出口级弹性{
起始:编号;
至:编号;
客户:任何;
输出:搜索输出;
错误:任何;
构造函数(主机:字符串、日志:字符串、elasticsearch:any){
this.client=new elasticsearch.client({
主持人:主持人,,
日志:日志
})
}
公共搜索({query,perPage,index,type,page}:SearchParams):SearchOutput{
让自我=这个;
这个是.client.search({
“索引”:索引,
“类型”:类型,
“身体”:{
“查询”:{
“布尔”:{
“必须”:{
“查询字符串”:{
“查询”:查询,
'默认_运算符':'和',
“模糊性”:1
}
},“过滤器”:[]
}
}
},
“大小”:每页,
“发件人”:0
},
(错误,响应)=>{
常量{hits,hits:{total}}=response
const lastPage=Math.ceil(总计/每页)
常量from=(每页*每页)-每页+1
常数至=(第页<最后一页)?第*页每页:总计
让输出={
总计:总计,
每页:每页,
当前页面:第页,
最后一页:最后一页,
from:from,,
致:致,,
数据:[]
}
如果(总数>=1){
for(点击中的常量键。点击){
if(hits.hits.hasOwnProperty(键)){
输出.数据.推送(点击.点击[键]。\u来源);
}
}
}否则{
output.data=[];
}
self.output=输出;
}
);
console.log(self);//输出{client:esapclient,output:SearchOutput Objest with all value}
console.log(self.output)//输出未定义
返回自我输出;
}
}

控制台移动到回调中,如下所示:

    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);
        }
    );
现在您可以这样调用搜索:

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

您有一个竞争条件,可能是在执行回调之前进行console.logging。看起来您正在为self.output设置初始值,但是您确定它们是您期望的值吗?我想我理解您的意思。我正在努力将回调结果返回到方法中,但不知道如何处理。由于希望方法返回的数据是异步返回的,因此需要执行一些操作,例如向搜索方法添加回调参数。我将编辑我的答案,并添加一个如何处理它的例子。非常感谢你的时间和帮助。
search(
    {'somequery', 'someperPage', 'someindex', 'sometype', 'somepage'}, 
    (data) => console.log(data)
)