javascript - 如何使用 reactjs 中的 graphql 在数据库中插入数据?
问题描述
我正在尝试使用 graphql 将数据保存在 db 中,但我遇到了错误。
错误
{
"errors": [
{
"message": "Syntax Error: Expected Name, found String \"action\"",
"locations": [
{
"line": 4,
"column": 32
}
],
"extensions": {
"code": "GRAPHQL_PARSE_FAILED"
}
}
]
}
filterStr 数据:
{"action":"count","demography":{"email_domain":"","age":{"from":"11-13-1919","to":"11-13-2018"},"gender":["male","female"],"company":"","phone":"","source":"","forms_submitted":"","added_by":"","education":"","occupation":"","interest":"","location":[],"paying_customer":""},"email":{"sent":[],"delivered":[],"opened":[],"clicked":[]}}
代码:
const USER_TOKEN = sessionStorage.getItem("user_token");
const audienceName = JSON.stringify(this.state.audienceName);
const userId = sessionStorage.getItem("id");
const filterStr = JSON.stringify(filter);//without stringify it is insering as [Object, Object]
let dataQuery = {
query: `mutation{
createAudience(input:{data:{
audience_name:${audienceName},
filters:${filterStr},
created_by: ${userId},
}}){
audience{
id
}
}
}`
}
const add_new_audience = {
method: methodType,
url: constants.graphql,
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${USER_TOKEN}`
},
data: JSON.stringify(dataQuery)
};
const resp = await axios(add_new_audience);
console.log(resp);
if(resp) {
this.props.history.push("/audiences");
}
谁能帮我在哪里需要更改代码?
解决方案
推荐阅读
- javascript - JS Promises.all 问题
- tableau-api - 计算工具提示中多个单独度量值的列百分比
- google-chrome - 使用批处理文件启动 Powerapp 打开带有恢复消息的空白 Chrome 页面
- python - 如何使用 matplotlib 在同一个图中绘制多条线,每条线代表目标组?
- perl - 在“carton install”之后,一些依赖项的版本为“0”
- ios - 在用户默认值中保存包含编码为数据的敏感数据的对象
- html - HTML -webkit-动画/动画
- three.js - 三个 js 对象旋转 y 旋转 z 代替
- apache-spark - 本地模式下的 Spark 执行器和随机播放
- python - 如何在 Tkinter 的函数中配置 canvas.create_text()?