我有一个带有Node后端的React前端,我要做的就是使用Facundo Olano的Google-Play-Scraper从Google Play商店中抓取一些数据。
这是我的React前端...
import React, {Component} from 'react';
import './App.css';
export default class App extends Component {
state = {
title: '',
review: ''
};
componentDidMount() {
this.callApi()
.then(res => this.setState({
title: res.title,
review: res.review
}))
.catch(err => console.log('Error: ', err));
}
callApi = async () => {
const response = await fetch('/gPlay');
const body = await response.json();
if (response.status !== 200) {
throw Error(body.message);
}
return body;
};
render() {
console.log("==================== app log ====================");
console.log("State: ", this.state);
return (
<div className="App">
<p>Title: {this.state.title}.</p>
<p>Review: {this.state.review}.</p>
</div>
);
}
}
这是我的Node后端...
[const express = require('express');
const app = express();
const port = process.env.PORT||5000;
const gPlay = require('google-play-scraper');
const appToScrape = 'com.mojang.minecraftpe';
app.get('/gPlay', (req, res) => {
const gPlayResults = gPlay.app({appId: appToScrape});
console.log('==================== server log ====================');
gPlayResults.then(console.log);
res.send({
title: 'title',
review: 'review',
});
});
app.listen(port, () => console.log(`Listening on port: ${port}`));
这是我得到的结果的一小段...
{
price: 6.99,
free: false,
currency: 'USD',
priceText: '$6.99',
offersIAP: true,
size: 'Varies with device'
}
我正在获得文档的全部结果,但是我需要能够拆分返回的对象,以便能够将其发送回前端,但是我终生无法我当中的人开始工作。
我以为我可以简单地做类似gPlayResults.price
的操作,并返回6.99,但是返回的结果是不确定的。
请有人帮忙,我一定很想念某些东西,因为它一定不那么难!!
感谢xxx
答案 0 :(得分:0)
好,我解决了! :)
对于感兴趣的任何人, $query = "INSERT INTO patients
(ID,
pet_name,
breed,
colour,
sex,
date_of_birth,
microchip_tatoo,
comment,
owner_ID)
VALUES
(NULL,
:pet_name,
:breed,
:colour,
:sex,
:date_of_birth,
:microchip_tatoo,
:comment,
:owner_ID)";
$query_params = array(':pet_name' => $pet_name,
':breed' => $breed,
':colour' => $colour,
':sex' => $sex,
':date_of_birth' => $date_of_birth,
':microchip_tatoo' => $microchip_tatoo,
':comment' => $comment,
':owner_ID' => $_SESSION['ID']);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
$check = true;
}catch(PDOException $ex){
$check = false;
die("Failed to run query: " . $ex->getMessage());
}
?>
被分配为gPlayResults
,而不是我期望的对象。
我使用 Promise
进行了回调,并能够使用将有用的对象发送回前端所需的结果。
.then
接下来的问题...