所以我想用我的mongo db在react中使用它,它很小,所以它不会超过react。我的烧瓶看起来像这样
import subprocess
from flask import Flask, request, jsonify, json
from flask_cors import CORS, cross_origin
import pymongo
disaster = ""
app = Flask(__name__)
CORS(app, support_credentials=True)
client = pymongo.MongoClient("NOT IMPORTANT FOR GITHUB")
db = client["twitterdb"]
col = db["tweets"]
our_mongo_database_compressed = col.find({},{'user.created_at':1, 'user.location':1,'_id':0})
def request_tweets(disaster):
print(disaster)
#subprocess.call("./../../../backend/get_tweets", disaster)
@app.route('/refresh_data', methods=['GET', 'POST'])
#@cross_origin(supports_credentials=True)
def refresh_data():
disaster = request.get_json()
request_tweets(disaster)
x = 0
y = []
for datas in our_mongo_database_compressed:
y.append(datas)
if(x > 100):
break
x+=1
#print(y)
return str(y)我的react函数看起来像这样
this.setState({
disaster: event.target.value
})
axios.post('http://localhost:5000/refresh_data', [this.state.disaster])
.then(function(response){
console.log(JSON.parse(response.data));
})
}我一直收到“JSON中位置2处的意外令牌”,我只希望将数据发送到react
发布于 2020-12-02 20:05:32
所以我把它弄明白了,我希望将来有这个问题的任何人都能看到这个。
for datas in our_mongo_database_compressed:
y.append(datas)这将创建一个字典数组。因此,y"location“将是从该数组中获取元素的方式。
考虑到这一点,我们需要将此数组更改为JSON字符串,这是一种用于在flask和react之间传输的数据类型,因此您将返回
return json.dumps(y)现在,您可能认为这意味着当您编写代码时,React中有一个字符串
JSON.parse(response.data)不是的。这太简单了,你有一个秘密的字符串的响应对象。因此,您需要使用JSON stringify将响应对象更改为字符串
JSON.parse(JSON.stringify(response.data))现在,您的json已经在react中了。
https://stackoverflow.com/questions/65100135
复制相似问题