TypeError:套接字不是构造函数

时间:2018-08-01 20:24:19

标签: node.js angular typescript constructor ftp-client

我是Angular / Node的新手,似乎无法找出我遇到的问题。

我正在尝试使用client-ftp连接到ftp,并且在实现方面遇到麻烦。本质上,我是在前端创建这样的按钮:

<button class="btn btn-primary" (click)="downloadFile()"><i class="fa fa-file-photo-o"></i> Download Screenshot</button>

并尝试通过这样的click事件来实现它:

downloadFile(){
    console.log('Connecting to sftp...');
    var ftpClient = require('ftp-client'),
        config = {
            host: 'localhost',
            port: 22,
            user: 'anonymous',
            password: 'anonymous'
        },
        options = {
            logging: 'basic'
        },
        client = new ftpClient(config, options);

    console.log('Downloading Screenshot...');
    client.connect(function () {
        client.download('/sftFilepPath', './Downloads', {
            overwrite: 'none'
        }, function (result) {
            console.log(result);
        });
    });
}

我一直遇到错误,但是每次按下按钮时都说:TypeError: Socket is not a constructor。我要在调试器中同时显示两个console.log()调用,所以我认为这与我尝试实际连接和下载的位置有关。

我尝试为FtpClient命名空间调用import并在构造函数中创建一个私有客户端参数并加以解决,但这似乎也不起作用。

我知道new关键字会隐式调用一个构造函数,但是我在代码的任何地方都没有引用“ Socket”。也许我想念什么?

这是我的导入和构造函数供参考:

import { Component, ElementRef, Input, OnInit, OnDestroy } from '@angular/core';
import { TestInstance, Test, TestDetails } from '../../objModules/index';
import { RunServices } from './run.services';
import { Observable } from 'rxjs/Rx';
import * as $ from 'jquery';
import * as _ from 'underscore';
import { ModalController } from './run.modal.controller';
// import { FtpClient } from 'ftp-client';

@Component({
    templateUrl: './run.modal.component.html',
    moduleId: module.id.toString(),
    selector: 'app-test-data',
    providers: [ RunServices ]
})

export class ModalComponent implements OnInit, OnDestroy {
    testInstance: TestDetails;
    id: string;
    private element: JQuery;

    private runId: string;
    private test$: Observable<Test>;
    private testHistory$: Observable<TestInstance[]>;

    private test: Test;
    private testHistory: TestInstance[];
    private selectedTest: TestInstance;
    private latestRuns: TestInstance[];

    private averagePass: number;
    private averageFail: number;

    services: RunServices;

    constructor(private modalController: ModalController, private el: ElementRef, private runServices: RunServices) {
        this.element = $(el.nativeElement);
        this.services = runServices;

        // Initialize these objects so we don't get any errors
        this.test = new Test(' ', ' ', ' ', [' '], ' ', ' ');
        this.testHistory = new Array<TestInstance>(new TestInstance(' ', ' ', ' ', 0, ' ', ' ', ' ', ' ', ' '));
        this.testInstance = new TestDetails(' ', ' ', ' ', ' ', ' ', ' ', ' ', 0, null, ' ');
        this.selectedTest = new TestInstance(' ', ' ', ' ', 0, ' ', ' ', ' ', ' ', ' ');
    }

2 个答案:

答案 0 :(得分:2)

我的猜测是您的ftp客户端太旧了。系统环境变化越来越快。但是,此客户端自2015年以来就没有进行过更新。请尝试使用最新的客户端,例如jsftp

答案 1 :(得分:1)

初始概述

client.download方法中您的文件路径存在异常。路径/stfFilepPath似乎有一个额外的'p'。除非它是路径的实际名称,否则请尝试将其删除。

所以;

client.download('/sftFilepPath', './Downloads'

成为

client.download('/sftFilePath', './Downloads'

尝试一下,让我知道它是否有效。如果没有,请尝试Lynx的解决方案。