从JSON文件定义Mongoose模式 [英] Defining a Mongoose schema from a JSON file

查看:157
本文介绍了从JSON文件定义Mongoose模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从JSON文件定义我的猫鼬模式.这是我的JSON文件结构:

I want to define my mongoose schema from 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..."
            }    
        ]}

这是我的node.js模型:

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);    

我尝试了从JSON数据"productJson"定义猫鼬模式的所有可能方法.但是,除非我预先定义了我的猫鼬模式,否则它将无法正常工作.有什么方法可以从模型中的JSON数据定义猫鼬模式?有什么建议吗?

I tried every possible way to define mongoose schema from JSON data 'productJson'. But unless I pre-define my mongoose schema, it is not working. Is there any way to define mongoose schema from JSON data in my model? Any suggestion please?

推荐答案

fs.readFile是一个异步函数,这意味着它立即返回,然后通过您作为第三个参数提供的回调函数将其结果提供给调用方

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

因此,您需要推迟使用productJson,直到其填充到该回调中.这意味着也要在回调内部移动模式和模型的创建.

As such, you need to hold off on using productJson until its populated within that callback. That means moving your schema and model creation inside the callback as well.

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 方法而是读取文件.这在诸如此类的启动/初始化情况下很有用,在这种情况下,整个应用程序只有在处理完此文件后才能继续进行.

Another alternative you can use here is to use the synchronous fs.readFileSync method to read the file instead. This is helpful in startup/initialization cases like this where your application as a whole shouldn't proceed until this file is processed.

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);

这篇关于从JSON文件定义Mongoose模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆