Defining Mongoose Schema from JSON File

I want to define my mongoose scheme from a JSON file. This is my JSON file structure:

   {    
        "default": [
            {
                "item": "productTitle",
                "label": "Product Title",
                "note": "e.g Samsung GALAXY Note 4",
                "type": "text",
                "required": "Product Name cannot be blank..."
            },
            {
                "item": "productCode",
                "label": "Product Code",
                "type": "text",
                "required": "Product Code cannot be blank..."
            }    
        ]}

This is my node.js model:

// Load the module dependencies
var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var fs = require('fs');
var file = __dirname + '/product.server.model.json';

// Read the json file
fs.readFile(file, 'utf8', function (err, data) {

data = JSON.parse(data);

var productJson = {};
for(var  i = 0; i < data.default.length; i++) {

    productJson[data.default[i].slug] = {
        type: 'String',
        required: data.default[i].required,
        default: '',
        trim: true
    }

}

});

// Define a new 'ProductSchema'
var ProductSchema = new Schema(
   // Here I want to put JSON Data 'productJson'
);

// Create the 'Product' model out of the 'ProductSchema'
mongoose.model('Product', ProductSchema);    

I tried all possible ways to determine the mongoose scheme from the JSON data 'productJson'. But if I do not predetermine my mongoose pattern, it does not work. Is there a way to determine the mongoose pattern from the JSON data in my model? Any suggestion please?

0
source share
1 answer

fs.readFile is an asynchronous function, which means that it returns immediately and then provides its results to the caller through the callback function that you provide as the third parameter.

, productJson, . .

fs.readFile(file, 'utf8', function (err, data) {

    data = JSON.parse(data);

    var productJson = {};
    for(var  i = 0; i < data.default.length; i++) {

        // Changed .slug to .item here as I don't see slug in the JSON
        productJson[data.default[i].item] = {
            type: 'String',
            required: data.default[i].required,
            default: '',
            trim: true
        }
    }

    // Define a new 'ProductSchema'
    var ProductSchema = new Schema(productJson);

    // Create the 'Product' model out of the 'ProductSchema'
    mongoose.model('Product', ProductSchema);
});

, , fs.readFileSync . /, , , .

var data = fs.readFileSync(file, 'utf8');
data = JSON.parse(data);

var productJson = {};
for(var  i = 0; i < data.default.length; i++) {

    // Changed .slug to .item here as I don't see slug in the JSON
    productJson[data.default[i].item] = {
        type: 'String',
        required: data.default[i].required,
        default: '',
        trim: true
    }
}

// Define a new 'ProductSchema'
var ProductSchema = new Schema(productJson);

// Create the 'Product' model out of the 'ProductSchema'
mongoose.model('Product', ProductSchema);
0

Source: https://habr.com/ru/post/1653867/


All Articles