Moorky
Moorky

Reputation: 35

TypeScript shows error when declaring a function

When executing my code, a typescript error appears that "require" is not a function, which is why I declared the function beforehand, but now typescript complains that "Modifiers cannot appear here.". Can someone help me?

import { Injectable, ValueProvider } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class DataService {
  edge : any;

  constructor() {
    declare function require(name: string); // "Modifiers cannot appear here."
    this.edge = require('edge');
  }

  getData(type: string, method: string) {
    var data = this.edge.func({
      assemblyFile: 'C:\Program Files\Common Files\Siemens\Automation\Simatic OAM\bin\CC.CommonInterfaces.dll',
      typeName: `CC.CommonInterfaces.${type}`,
      methodName: `${method}`
    });

    return data(null, function(error, result) {
      if (error) throw error;
      console.log(result);
      return result;
    });
  }

}

Upvotes: 2

Views: 471

Answers (1)

Rahul Dudhane
Rahul Dudhane

Reputation: 554

You can directly import edge instead of using require:

import { Injectable, ValueProvider } from "@angular/core";

import * as edge from "edge";

@Injectable({
  providedIn: "root"
})
export class DataService {
  constructor() {}

  getData(type: string, method: string) {
    let data = edge.func({
      assemblyFile:
        "C:Program FilesCommon FilesSiemensAutomationSimatic OAM\binCC.CommonInterfaces.dll",
      typeName: `CC.CommonInterfaces.${type}`,
      methodName: `${method}`
    });

    return data(null, function(error, result) {
      if (error) throw error;
      console.log(result);
      return result;
    });
  }
}

Upvotes: 3

Related Questions