Javascript Meteorjs处理从服务器到客户端的刮取数据返回未定义
我正在尝试为我的Meteor+React应用程序实现url预览,当用户在文本区域粘贴url时,他们将获得url预览。我计划通过使用几个npm模块来实现这一点,即: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
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中使用承诺的好文章: