TypeError: response. json не является функцией - PullRequest
0 голосов
/ 13 марта 2020

Я хотел бы загрузить свой результат запроса в API в формате JSON для моей базы данных postgresql.

Однако я получаю, что мой json не был определен:

TypeError: response. json не является функцией

Как загрузить эти данные в банк?

Я хотел бы загрузить данные обычным способом в SQL, а не в поле JSON, чтобы можно было выполнять запросы в будущем

index. js

require('es6-promise').polyfill();
require('isomorphic-fetch');

let content = {

    "query": `{
                squads {
                    name
                        cards(includedOnKanban: true, closed: false, archived: false, cancelled: false, updatedSince: \"2020-01-01T00:00:00-0300\") {
                            identifier
                            title
                            description
                            status
                            priority

                            assignees {
                                fullname
                                email
                              }

                        }
                    }
            }`

};

fetch('https://www.bluesight.io/graphql', {
method: 'POST',
headers: {
    'Content-Type': 'application/json',
    'Bluesight-API-Token': 'token-here'
},
body: JSON.stringify(content)
})
.then(response => {
    if (response.ok) {
      return response.json()
    } else {
      return Promise.reject('something went wrong!')
    }
  })
.then(response => console.log(response.data))
.then(response => console.log(JSON.stringify(response.data)));

const { Client } = require('pg');

const client = new Client({
    user: 'postgres',
    host: '127.0.0.1',
    database: 'dbbase',
    password: 'postgres',
    port: 5432,
})
client.connect();
client.query('INSERT INTO tb_bluesight (identifier,title,description,status,priority,fullname,email,date_insert) VALUES ($1, $2, $3, $4, $5, $6, $7, current_timestamp)', response => response.json());
client.end();

JSON

{
    "data": {
        "squads": [
            {
                "name": "SUPPORT IT",
                "cards": [
                    {
                        "identifier": "06x38y",
                        "title": "ALL - Validate data,
                        "description": "review database.",
                        "status": null,
                        "priority": "medium",
                        "assignees": [
                            {
                                "fullname": "Carlos",
                                "email": "carlos@br.it.com",
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

Таблица

CREATE TABLE tb_bluesight(
identifier varchar,
title varchar,
description varchar,
status varchar,
priority varchar,
fullname varchar,
email varchar,
date_insert timestamp
);

1 Ответ

0 голосов
/ 13 марта 2020
const data = fetch('https://www.bluesight.io/graphql', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Bluesight-API-Token': 'token-here'
    },
    body: JSON.stringify(content)
})
.then(response => {
    if (response.ok) {
        return response.json()
    } else {
        return Promise.reject('something went wrong!')
    }
});

const { Client } = require('pg');

const client = new Client({
    user: 'postgres',
    host: '127.0.0.1',
    database: 'dbbase',
    password: 'postgres',
    port: 5432,
});

client.connect();

// Example, replace with real values
const parameters = ['$1', '$2', '$3', '$4','$5', '$6', '$7'];

const query = "INSERT INTO tb_bluesight 
 (identifier,title,description,status,priority,fullname,email,date_insert) 
     VALUES ($1, $2, $3, $4, $5, $6, $7, CURRENT_TIMESTAMP)";

client.query(query, parameters, (err, res) => {
    if (err) {
        console.log(err.stack)
    } else {
        // res.rows will contain the inserted rows
        console.log(res.rows[0])
    }
});

client.end();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...