Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.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
Javascript Typescript数组变量不可访问_Javascript_Reactjs_Typescript - Fatal编程技术网

Javascript Typescript数组变量不可访问

Javascript Typescript数组变量不可访问,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,我需要一些帮助,以便能够从函数调用seachResults2返回myArray。当我做控制台日志时,编译器告诉我它找不到myArray。我希望能够将myArray返回给调用方 const searchResults2 = (query: string) => { const subscriptionKeyCredential = new atlasService.SubscriptionKeyCredential( "SUBSCRIPTION"

我需要一些帮助,以便能够从函数调用seachResults2返回myArray。当我做控制台日志时,编译器告诉我它找不到myArray。我希望能够将myArray返回给调用方

const searchResults2 = (query: string) => {
    const subscriptionKeyCredential = new atlasService.SubscriptionKeyCredential(
      "SUBSCRIPTION"
    );
    const pipeline = atlasService.MapsURL.newPipeline(subscriptionKeyCredential);
    const searchURL = new atlasService.SearchURL(pipeline);
    searchURL
      .searchAddress(atlasService.Aborter.timeout(10000), query)
      .then((response) => {
        const data2 = response.geojson.getFeatures();
       
        const myArray = new Array(data2.features.length)
          .fill(0)
          .map((item, idx) => {
            const category: string = data2.features[idx].id as string;
            return {
              value: category,
              label: labelElement(query, category),
            };
          });
      });
   console.log(myArray);  // Cannot find name 'myArray'
   //return myArray  cant do since its not accessable.
  };

const labelElement = (query: string, category: string) => {
    return (
      <div
        style={{
          display: "flex",
          justifyContent: "space-between",
        }}
      >
        <span>
          Found {query} on <a>{category}</a>
        </span>
        <span>{getRandomInt(200, 100)} results</span>
      </div>
    );
    }
它的名字是这样的

 const [options, setOptions] = useState<SelectProps<unknown>["options"]>([]);

  const handleSearch = (value: string) => {
    setOptions(value ? searchResults2(value) : []);
  };

这是一个关于变量范围的问题。

myArray变量不可访问当您尝试记录它时,您将它从其范围中注销

const searchResults2 = async (query: string) => {
        const subscriptionKeyCredential = new atlasService.SubscriptionKeyCredential(
          "SUBSCRIPTION"
        );
        const pipeline = atlasService.MapsURL.newPipeline(subscriptionKeyCredential);
        const searchURL = new atlasService.SearchURL(pipeline);
        let myArray;
        let response = await searchURL.searchAddress(atlasService.Aborter.timeout(10000), query);
        const data2 = response.geojson.getFeatures();
        myArray = new Array(data2.features.length)
            .fill(0)
            .map((item, idx) => {
                const category: string = data2.features[idx].id as string;
                return {
                  value: category,
                  label: labelElement(query, category),
                };
              }); 
       console.log(myArray);  
      };
    
    const labelElement = (query: string, category: string) => {
        return (
          <div
            style={{
              display: "flex",
              justifyContent: "space-between",
            }}
          >
            <span>
              Found {query} on <a>{category}</a>
            </span>
            <span>{getRandomInt(200, 100)} results</span>
          </div>
        );
        }

myArray变量不可访问当您尝试将其记录时,您已将其记录出其作用域

const searchResults2 = async (query: string) => {
        const subscriptionKeyCredential = new atlasService.SubscriptionKeyCredential(
          "SUBSCRIPTION"
        );
        const pipeline = atlasService.MapsURL.newPipeline(subscriptionKeyCredential);
        const searchURL = new atlasService.SearchURL(pipeline);
        let myArray;
        let response = await searchURL.searchAddress(atlasService.Aborter.timeout(10000), query);
        const data2 = response.geojson.getFeatures();
        myArray = new Array(data2.features.length)
            .fill(0)
            .map((item, idx) => {
                const category: string = data2.features[idx].id as string;
                return {
                  value: category,
                  label: labelElement(query, category),
                };
              }); 
       console.log(myArray);  
      };
    
    const labelElement = (query: string, category: string) => {
        return (
          <div
            style={{
              display: "flex",
              justifyContent: "space-between",
            }}
          >
            <span>
              Found {query} on <a>{category}</a>
            </span>
            <span>{getRandomInt(200, 100)} results</span>
          </div>
        );
        }

在这种情况下,您可以尝试使用async/away并从承诺中返回myArray,它将自动包装在承诺中

const searchResults2 = async (query: string) => {
    const subscriptionKeyCredential = new atlasService.SubscriptionKeyCredential(
        "SUBSCRIPTION"
    );
    const pipeline = atlasService.MapsURL.newPipeline(subscriptionKeyCredential);
    const searchURL = new atlasService.SearchURL(pipeline);
    let myArray = await searchURL
            .searchAddress(atlasService.Aborter.timeout(10000), query)
        .then((response) => {
            const data2 = response.geojson.getFeatures();

            return new Array(data2.features.length)
                .fill(0)
                .map((item, idx) => {
                    const category: string = data2.features[idx].id as string;
                    return {
                        value: category,
                        label: labelElement(query, category),
                    };
                }); // you can keep it as const but log it in this level
        });
        
        
    console.log(myArray); 
};

在这种情况下,您可以尝试使用async/away并从承诺中返回myArray,它将自动包装在承诺中

const searchResults2 = async (query: string) => {
    const subscriptionKeyCredential = new atlasService.SubscriptionKeyCredential(
        "SUBSCRIPTION"
    );
    const pipeline = atlasService.MapsURL.newPipeline(subscriptionKeyCredential);
    const searchURL = new atlasService.SearchURL(pipeline);
    let myArray = await searchURL
            .searchAddress(atlasService.Aborter.timeout(10000), query)
        .then((response) => {
            const data2 = response.geojson.getFeatures();

            return new Array(data2.features.length)
                .fill(0)
                .map((item, idx) => {
                    const category: string = data2.features[idx].id as string;
                    return {
                        value: category,
                        label: labelElement(query, category),
                    };
                }); // you can keep it as const but log it in this level
        });
        
        
    console.log(myArray); 
};

我不认为这是一个异步调用问题,它更多的是一个变量声明范围问题。数组正在geojson调用中分配其长度。我需要帮助在调用外部声明myarray,在调用内部分配myarray的长度,在调用内部填充数组,然后在geojson调用外部使其可用。我认为这不是异步调用问题,而是变量声明范围问题。数组正在geojson调用中分配其长度。我需要帮助在调用外部声明myarray,在调用内部分配myarray的长度,在调用内部填充数组,然后在geojson调用外部使其可用。感谢User222,是的,这是我所想的,但我不知道如何使其工作。现在核对你的答案。。检查后几分钟内会给你回复。哼,这个级别的数组是空的。在范围内,它具有值。外部后的行中myArray为空,内部后的行中有值。console.loginside;console.logmyArray;};控制台。外部登录;console.logmyArray;//找不到名称“myArray”//返回myArray,因为它不可访问。};我已经更新了我的响应,请检查它。当您调用它时,在调用函数中使用wait以及wait searchResults2和async非常感谢您,const handleSearch=async value:string=>{setOptionsvalue?wait searchResults2value:[];};我成功了。。我希望我有办法给你10颗星。。。谢谢,非常感谢。谢谢。是的,我是这么想的,但是我想不出怎么做。现在核对你的答案。。检查后几分钟内会给你回复。哼,这个级别的数组是空的。在范围内,它具有值。外部后的行中myArray为空,内部后的行中有值。console.loginside;console.logmyArray;};控制台。外部登录;console.logmyArray;//找不到名称“myArray”//返回myArray,因为它不可访问。};我已经更新了我的响应,请检查它。当您调用它时,在调用函数中使用wait以及wait searchResults2和async非常感谢您,const handleSearch=async value:string=>{setOptionsvalue?wait searchResults2value:[];};我成功了。。我希望我有办法给你10颗星。。。谢谢你,非常感谢你。谢谢斯拉瓦,现在就尝试一下,。。将在尝试此操作后5分钟内返回..好的,因此它现在在函数外部可用,但它作为对调用函数的承诺{}。请参阅文章中的通话时间设置选项值?searchResults2value:[];它给出了一条红色的斜线,上面有一个错误:“Promise”类型不能分配给“SetStateAction”类型。感谢斯拉瓦,现在尝试一下,。。将在尝试此操作后5分钟内返回..好的,因此它现在在函数外部可用,但它作为对调用函数的承诺{}。请参阅文章中的通话时间设置选项值?searchResults2value:[];它给出了一条带错误的红色斜线:“Promise”类型不可分配给“SetStateAction”类型。