Difference in Java and JS when creating a class and instantiating it?

2

I am using JS to perform an API and as much as I try I can not understand why, when making a POST request to create a user, it tells me that User is not a constructor.

I am "new" in JS and I have in mind the creation of classes / constructors that use Java, so I do not understand why in JS it does not work in the same way . Maybe I miss something or maybe I do not understand it well .

I have attached the code with which I am working as an example to try to understand it:

As a database I use MONGO

'use strict'

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var usuarioSchema = Schema({
    name: String,
    surname: String,
    companyName: String
    //id: Integer,
    //telephone: Integer,
    //data: String

});
module.export = mongoose.model('Usuario', usuarioSchema);

This would be the file where I supposedly kept the user in my database.

'use strict'

var Usuario = require('../Usuario.js');

function saveUsuario(request, res) {
    var usuario = new Usuario(); 
    var params = request.body; 


    usuario.name = params.name;
    usuario.surname = params.surname;
    usuario.companyName = params.companyName;


    usuario.save(function (err, usuarioStored) {
        if (err) { //Si se producen errores al guardar un usuario
            res.status(500).send({
                message: 'Error al guardar el usuario'
            });
        } //Si no se producen errores al guardar
        res.status(200).send({
            usuario: usuarioStored
        });

    });
}

By using some HTTP request software (I use POSTMAN ) it tells me that User is not a constructor. I can not understand why it is not a constructor if I am actually saving a new User in my database with the three specified fields.

    
asked by Aag 18.03.2017 в 13:24
source

2 answers

1
  

I do not understand why JS does not work in the same way

Why should I do it? They are completely different languages that were born with needs and that fulfill different objectives. It's like comparing Pears with Apples and saying why they do not taste the same if both are fruits.

Java implements the OOP in a very different way to JavaScript. The latter implements a prototyped OOP, which is very different from the classic OOP that implements Java. In fact, in JavaScript there is no concept of classes .

After clarification, let's go for your mistake. Your error is that you are not exporting anything. Node.js implements CommonJS in its source code, which is a library for code modularization, allowing you to write JavaScript code in independent "modules", which can be exported and imported from anywhere. Without the existence of CommonJS, you would have to write everything in a single file , greatly complicating post-development tasks.

To export a module in CommonJS use module.exports and not module.export as you are using :

 module.exports = moongose.model('Usuario', usuarioSchema);

This puts the file in the CommonJS repository so it can be imported. In your case you are not exporting anything, so, when you import Usuario , you are importing the CommonJS object and this is not a constructor .

Also remember that this way an object is exported so that it is available by default . There is also the following way to export:

var model = moongose.model('Usuario', usuarioSchema);
exports.model = model;

In this case, model will not be available as the default option to import, but the entire export object containing model will be exported:

var Usuario = require('../Usuario').model;

Note: It is not necessary to add the .js suffix when importing.

    
answered by 18.03.2017 / 13:59
source
0

According to the previous post Java is object oriented, Javascript is a language based on prototypes.

A POJO is an object that instances (if it is not marked as abstract) and has characteristics (private encapsulated attributes) and behaviors that are methods that can be public or private, when you create the object using its constructor, you can use its methods public.

Example:

MyObject myObject = new MyObject ();

myObject.myInstanceMethod ();

In javascript you create prototypes that you attach to "objects", for example: If you want to create a new functionality for the "object" JavaScript String you would do something like this:

String.prototype.isEmpty = function () {     return (this.length === 0 ||! this.trim ()); };

And you can use it like this:

"". isEmpty (); < - Returns true

    
answered by 18.03.2017 в 15:39