Home > OS >  TypeError: Class constructor Wia cannot be invoked without 'new'
TypeError: Class constructor Wia cannot be invoked without 'new'

Time:11-16

This is my code inside a json file, whenever I try to run it I get this error: TypeError: Class constructor Wia cannot be invoked without 'new' at Object. (/home/pi/wia-pi-camera/run-camera.js:3:25)

'use strict';

var wia = require('wia')('your-device-secret-key');
var fs = require('fs');
var RaspiCam = require("raspicam");

// Setup the camera
var camera = new RaspiCam({
  mode: 'photo',
  output: __dirname   '/photo.jpg',
  encoding: 'jpg'
});

// Listen for the "start" event triggered when the start method has been successfully initiated
camera.on("start", function(){
  console.log("Starting to take photo.");
});

// Listen for the "read" event triggered when each new photo/video is saved
camera.on("read", function(err, timestamp, filename){
  console.log("New photo created.", timestamp, filename);

  // Publish the photo to Wia
  wia.events.publish({
    name: 'photo',
    file:  fs.createReadStream(__dirname   '/'   filename)
  });
});

// Take a photo
camera.start();

The second line is where the error seems to occur: var wia = require('wia')('my_device_secret_key');

Does anyone know how to fix this?

CodePudding user response:

Try it like this:

const Wia = require('wia');
var wia = new Wia('your-device-secret-key');

require('wia') returns a class, you need to call that with new to create an instance.

  • Related