Home > Mobile >  How to fix: Cannot read properties of undefined (reading 'Promise')?
How to fix: Cannot read properties of undefined (reading 'Promise')?

Time:09-19

I am trying to ping and ip address/url with npm ping in a nestJs project, once I send the get request I get a 500 error response and this error in netsJs:

[Nest] 17732 - 09/19/2022, 3:47:16 AM ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'promise') TypeError: Cannot read properties of undefined (reading 'promise')

Any Insight?

//ping.controller.ts

import { Controller, Get } from '@nestjs/common';
import { PingService } from './ping.service';

@Controller('ping')
export class PingController {
  constructor(private pingService: PingService) {}
  @Get()
  pinger() {
    return this.pingService.ping();
  }
}

//ping.service.ts

import { Injectable } from '@nestjs/common';
import ping from 'ping';
@Injectable()
export class PingService {
  async ping() {
    const result = await ping.promise.probe('192.168.43.73', {
      timeout: 10,
      extra: ['-i', '2'],
    });

    return console.log(result.alive);
  }
}

error Image error Image

CodePudding user response:

Your import statement is wrong:

import ping from 'ping';

There is no global export named ping provided by the library. You should either do:

import * as ping from 'ping'; <-- if you want to import everything

or

import { promise } from 'ping'; Just import promise, if only that is required
  • Related