Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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 Meteorjs处理从服务器到客户端的刮取数据返回未定义_Javascript_Reactjs_Web Scraping_Meteor - Fatal编程技术网

Javascript Meteorjs处理从服务器到客户端的刮取数据返回未定义

Javascript Meteorjs处理从服务器到客户端的刮取数据返回未定义,javascript,reactjs,web-scraping,meteor,Javascript,Reactjs,Web Scraping,Meteor,我正在尝试为我的Meteor+React应用程序实现url预览,当用户在文本区域粘贴url时,他们将获得url预览。我计划通过使用几个npm模块来实现这一点,即: ,及 我理解为了避免任何CORS问题,请求应该在服务器端完成。 因此,我目前已经设置了: //客户 import urlRegex from 'url-regex'; const onTextareaChange = e => { let value = e.target.value; let testURL

我正在尝试为我的Meteor+React应用程序实现url预览,当用户在文本区域粘贴url时,他们将获得url预览。我计划通过使用几个npm模块来实现这一点,即:

  • ,及
  • 我理解为了避免任何CORS问题,请求应该在服务器端完成。 因此,我目前已经设置了:

    //客户

    import urlRegex from 'url-regex';
    const onTextareaChange = e => {
        let value = e.target.value;
        let testURL = urlRegex().test(value) //returns true if url exists in textarea
        console.log(testURL);
        if(testURL){
            let extractURL = value.match(urlRegex()) //extract the url
            extractURL.map(url =>{
                console.log(url)
                Meteor.call('scrapeURL',{url}, function (result){
                     console.log(result)         
                })
    
            })
        }
        /* console.log(e.target.value) */
        setTextarea(e.target.value)
     }
    
    //在服务器上

    import ogs from 'open-graph-scraper';
    /* 24. scrapeURL */
    'scrapeURL' ({url}){
      new SimpleSchema({
        url : { type  : String }
      }).validate({url})
      if(!Meteor.userId){
        throw new Meteor.Error('not-authorised!')
      } else {
        let options = { 'url': url };
          ogs(options)
            .then(function (result) {
              console.log('result:', result);
              return result;
            })
            .catch(function (error) {
              console.log('error:', error);
            });
      }
    }    
    
    这里的问题是,当我尝试在服务器上
    console.log
    记录
    results
    时,刮取的数据显示在服务器控制台中。但是,当我试图将
    结果
    从服务器返回到客户端时,客户端上的
    console.log
    显示
    undefined


    我不知道代码出了什么问题。

    您的scrapeUrl函数不返回任何数据(您只描述了
    .then()
    函数将返回的内容),您应该这样尝试:

    import ogs from 'open-graph-scraper';
    /* 24. scrapeURL */
    'scrapeURL' ({url}){
      new SimpleSchema({
        url : { type  : String }
      }).validate({url})
      if(!Meteor.userId){
        throw new Meteor.Error('not-authorised!')
      } else {
        let options = { 'url': url };
    
        // here return the full promise : 
        return ogs(options)
            .then(function (result) {
              console.log('result:', result);
              return result;
            })
            .catch(function (error) {
              console.log('error:', error);
              // probably here a need to tell the client that there was an error
              //throw new Meteor.Error(error);
            });
      }
    }   
    
    以下是一篇关于在Meteor中使用承诺的好文章:

    您的scrapeUrl函数不返回任何数据(您只描述了
    .then()
    函数将返回的数据),您应该尝试以下方法:

    import ogs from 'open-graph-scraper';
    /* 24. scrapeURL */
    'scrapeURL' ({url}){
      new SimpleSchema({
        url : { type  : String }
      }).validate({url})
      if(!Meteor.userId){
        throw new Meteor.Error('not-authorised!')
      } else {
        let options = { 'url': url };
    
        // here return the full promise : 
        return ogs(options)
            .then(function (result) {
              console.log('result:', result);
              return result;
            })
            .catch(function (error) {
              console.log('error:', error);
              // probably here a need to tell the client that there was an error
              //throw new Meteor.Error(error);
            });
      }
    }   
    
    以下是一篇关于在Meteor中使用承诺的好文章: