首页 > 解决方案 > 如何使用 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");
}

谁能帮我在哪里需要更改代码?

标签: javascriptnode.jsreactjsgraphqlexpress-graphql

解决方案


推荐阅读