【问题标题】:TypeError: Socket is not a constructorTypeError:套接字不是构造函数
【发布时间】:2019-01-09 12:34:21
【问题描述】:

我是 Angular/Node 的新手,似乎无法弄清楚我遇到的问题。

我正在尝试使用 client-ftp 连接到 ftp,但在实施时遇到了问题。本质上,我正在像这样在前端创建一个按钮:

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

并尝试通过像这样的点击事件来实现它:

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, ' ', ' ', ' ', ' ', ' ');
    }

【问题讨论】:

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


    【解决方案1】:

    初步概述

    client.download 方法中的文件路径存在异常。路径/stfFilepPath 似乎有一个额外的“p”。尝试删除它,除非那是路径的实际名称。

    所以;

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

    变成

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

    尝试一下,如果它有效,请告诉我。如果没有,请尝试 Lynx 的解决方案。

    【讨论】:

    • 我列出的文件路径不是我尝试使用的实际路径。如果是这种情况,我很确定这不会导致我面临的错误。我确实在我的代码中仔细检查了我的文件路径,它们看起来确实正确。
    【解决方案2】:

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

    【讨论】:

    • 请不要忘记将此答案标记为解决方案,如果它解决了您的问题或回答了您的问题。
    • 这很公平。我将尝试 jsftp,如果它有效,我会相应地标记答案。
    • 我似乎仍然无法让它工作。现在,当单击事件被触发时,我收到错误“TypeError:createConnection 不是函数”。我使用 jsftp here 发布了另一个问题。
    • 浏览像这样的一些帖子 github.com/browserify/browserify/issues/1780 我猜出于安全原因,基于 JS 的 FTP 客户端不应该在浏览器中运行。您必须建立自己的服务器并在那里实现客户端。只有这样您才能访问浏览器无法访问的 Net 模块并导致您提到的错误。这似乎是一个普遍问题。
    猜你喜欢
    • 2019-08-14
    • 2023-03-03
    • 2018-06-27
    • 2016-04-16
    • 2021-11-26
    • 2019-10-27
    • 2018-07-18
    相关资源
    最近更新 更多