使用Angular 6和Typescript进行编译时,HTTP post方法给出了错误

时间:2018-07-24 14:49:18

标签: angular typescript angular6

我创建了一个Angular项目,将数据添加到Neo 4j数据库中。我已经使用Angular 6和TypeScript完成了基本任务。但是编译发生问题。第一次尝试使用“ ng serve”命令为应用程序提供服务时,它将出现以下错误并提示“编译失败”。

'ERROR in src/app/app.component.ts(274,26): error TS2554: Expected 0-2 arguments, but got 4.
src/app/app.component.ts(339,26): error TS2554: Expected 0-2 arguments, but got 3.'

但是,当我删除注释中的字母或对注释进行任何其他更改时,它将实时重新编译,即使它给出了以上错误消息,也可以成功编译。但是问题是我想使用“ ng build”来构建应用程序以将其部署在服务器中。当我尝试构建它时,它总是会失败,并给出上述错误消息。

以下是问题的屏幕截图。 enter image description here

这是我的进口货,

import {Component, OnInit} from '@angular/core';
import APP_CONFIG from './app.config';
import {Node, Link} from './d3';
import {NEODictionary} from './d3/models/dictionary';
import {Http, RequestOptions, Response, Headers} from '@angular/http';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Observable} from 'rxjs/index';

这是我的构造函数,

constructor(private http: HttpClient) {
        this.ontologyDropdown = 'All';
        this.relSourceDropdown = 'select';
        this.relDirectionDropdown = 'nodirection';
        this.relDestinationDropdown = 'select';
        this.setJsonData('All');

        this.onEditCloseItems();
        this.fieldArray.push({name: 'name', value: ''});
    }

这是我的代码,

async addNodeToDB() {
        if ((this.nodeLabel == null) || (this.nodeLabel  === '' )) {
            alert('Please insert a Node Label!')
        } else if (this.fieldArray.length === 0) {
            alert('Please insert the name of the node!')
        } else if (this.fieldArray[0].value === '') {
            alert('Please insert the name of the node!')
        } else {
            const node_properties = [];
            const node_info = {};

            for (let i = 0; i < this.fieldArray.length; i++) {
                const node_prop = {'prop_name': this.fieldArray[i]['name'], 'prop_value': this.fieldArray[i]['value']};
                node_properties.push(node_prop)
            }
            const postbody = {
                'nodeInfo': {
                    'name': this.nodeLabel,
                    'properties': node_properties
                }
            };
            console.log(postbody);
            const addNodeUrl = 'http://localhost:8080/ontology/node_info';
            const headers = new HttpHeaders();
            headers.append('Content-Type', 'application/json');
            //This is the line referred to as (274,26) in error message
            return await this.http.post(addNodeUrl, postbody, {headers: headers}).toPromise()
                .then(this.getDomainDatabase(this.nodeLabel),
                    this.setDropDownNodes(this.ontologyDropdown),
                    // AppComponent.extractData,
                    alert('Node added successfully!!!'),
                    this.clearPropertyList())
                .catch();
        }
    }




async addRelationshipToDB() {
        if (this.relSourceDropdown === 'select') {
            alert('Please select a source node!');
        } else if ((this.relType == null) || this.relType === '') {
            alert('Please insert the relationship type!');
        } else if (this.relDirectionDropdown === 'nodirection') {
            alert('Please select a relationship direction!');
        } else if (this.relDestinationDropdown === 'select') {
            alert('Please select a destination node!');
        } else {
            const rel_properties = [];
            const rel_info = {};

            for (let i = 0; i < this.fieldArrayRel.length; i++) {
                const rel_prop = {
                    'prop_name': this.fieldArrayRel[i]['name'],
                    'prop_value': this.fieldArrayRel[i]['value']
                };
                rel_properties.push(rel_prop)
            }
            const postbody = {
                'relationshipInfo': {
                    'rel_name': this.relType,
                    'source': {
                        'name': this.nodeLabel,
                        'properties': [{
                            'prop_name': 'name',
                            'prop_value': this.relSourceDropdown
                        }]
                    },
                    'destination': {
                        'name': this.nodeLabel,
                        'properties': [{
                            'prop_name': 'name',
                            'prop_value': this.relDestinationDropdown
                        }]
                    },
                    'rel_properties': rel_properties,
                    'direction': this.relDirectionDropdown
                }
            };
            const addRelUrl = 'http://localhost:8080/ontology/rel_info';
            const headers = new HttpHeaders();
            headers.append('Content-Type', 'application/json');
            //This is the line referred to as (339,26) in error message
            return await this.http.post(addRelUrl, postbody, {headers: headers}).toPromise()
                .then(this.getDomainDatabase(this.nodeLabel),
                    // AppComponent.extractData,
                    alert('Relationship added successfully'),
                    this.clearRelForm())
                .catch();
        }
    }

但是,我尝试在两个地方也都删除了{headers:headers}参数,但是它仍然给出相同的错误。该应用程序运行正常,但由于这个问题我无法构建它。

0 个答案:

没有答案