1

I'm trying to implement some oop in my javascript program, so basically I have this project structure

src
|_model
|    |_Amministratore.js
|_main.js

I have the following code in Amministratore.js

///Rappresenta un amministratore di sistema
const Storage = require("../PersistanceStorage");

function Amministratore(info) {

  this.email = info.email;
  this.password = info.password;
  this.nome = info.nome;
  this.cognome = info.cognome;
  this.role = "admin";

  this.aggiungiID = function(ID) {
    this.ID = ID;
  };

  //Ritorna il path del nodo
  this.path = function() {
    return "Admin";
  };

  //Restituisce la versione memorizzabile nel
  this.getUploadableVersion = function() {
    return {
      [ID]: {
        nome: this.nome,
        cognome: this.cognome
      }
    };
  };

Now in my main.js I have this piece of code:

exports.postAdmin = functions.region(REGION).https.onRequest((req, res) => {

  const adminData = req.body;

  if (adminData === null || adminData === undefined) {
    return res
      .status(400)
      .send(impacchettaInformazioni(null, "must provide a body!", 400));
  }

  const admin = new model.Amministratore(adminData);

  return admin
    .postAmministratore()
    .then(function() {
      return res
        .status(200)
        .send(impacchettaInformazioni(null, "Admin created!", 200));
    })
    .catch(err => {
      return rej
        .status(500)
        .send(impacchettaInformazioni(null, err.message, "500"));
    });

});

But I keep getting this error:

TypeError: model.Amministratore is not a constructor
    at exports.postAdmin.functions.region.https.onRequest (/srv/index.js:311:17)
    at cloudFunction (/srv/node_modules/firebase-functions/lib/providers/https.js:57:9)
    at /worker/worker.js:783:7
    at /worker/worker.js:766:11
    at _combinedTickCallback (internal/process/next_tick.js:132:7)
    at process._tickDomainCallback (internal/process/next_tick.js:219:9)

Am I missing something? Do I need some kind of module.export like when I export some functions inside another file? Thanks for the help.

2
  • 1
    Where is that model coming from and what is model.Amministratore? Commented Oct 7, 2019 at 13:34
  • Exactly, You need to export functions Commented Oct 7, 2019 at 13:43

1 Answer 1

1

You need to export the constructor function at Amministratore.js

exports.Amministratore = Amministratore

Then require it at your main.js file

const model = require('./model/Amministratore.js')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.