N
N
nathan1117772019-07-01 13:07:46
JavaScript
nathan111777, 2019-07-01 13:07:46

Two questions of node.js code in which I fill the database with goods?

There is such a node.js code of two files with which I fill the database with goods.
the file in which I create the model:

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

var schema = new Schema({
    imagePath: {type: String, required: true},
    title: {type: String, required: true},
    description: {type: String, required: true},
    price: {type: Number, required: true}
});

module.exports = mongoose.model('Product', schema);
A constructor was created here, but the documentation says that a constructor is any function, spitting through the word "new".
First question:
But there is no function in this code, then it turns out that it is not a constructor? or how? or what is ----------------------------------------------------- -------------------------------------------------- ---------------------------------------- There is another file, which imports the "product" model from the first file, and here is the code of the other file:var schema = new Schemavar schema = new Schema?
var Product = require('../models/product');

var products = [
    new Product({
        imagePath: 'https://upload.wikimedia.org/wikipedia/en/5/5e/Gothiccover.png',
        title: 'Gothic Video Game',
        description: 'Awesome Game!!!!',
        price: 10
    }),
    new Product({
        imagePath: 'http://eu.blizzard.com/static/_images/games/wow/wallpapers/wall2/wall2-1440x900.jpg',
        title: 'World of Warcraft Video Game',
        description: 'Also awesome? But of course it was better in vanilla ...',
        price: 20
    }),
    new Product({
        imagePath: 'https://support.activision.com/servlet/servlet.FileDownload?file=00PU000000Rq6tz',
        title: 'Call of Duty Video Game',
        description: 'Meh ... nah, it\'s okay I guess',
        price: 40
    })

And the second question:
What is here:
var products = [];
I understand that this is an array, but an array of what? We can say that this is an array of several new Product, but I cannot explain what new Product is, I can only explain that it contains properties with values.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Sviridov, 2019-07-01
@dimuska139

All that comes with new is the creation of a new object (class instance). Accordingly, new Product - creation of a new Product object. It turns out that products is an array of Product objects.
var schema = new Schema - similarly, creating an instance of the Schema class.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question