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:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var fs = require('fs');
var file = __dirname + '/product.server.model.json';
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
}
}
});
var ProductSchema = new Schema(
);
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?
source
share