【问题标题】:Defining a Mongoose schema from a JSON file从 JSON 文件定义 Mongoose 模式
【发布时间】:2015-01-07 17:04:45
【问题描述】:

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

   {    
        "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 模型:

// 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 数据定义猫鼬模式?请问有什么建议吗?

【问题讨论】:

  • 除了slug 需要改为item 之外,如果您将productJson 传递给Schema 构造函数,它看起来会起作用。有什么不好的地方?
  • 如果我将 productJson 传递给 Schema 构造函数,则显示“productJson 未定义”的错误。因为 node.js 是一个异步进程。如何在ProductSchema 中使用productJson?
  • 好的,由于缺少缩进,我没有看到 productJson 在 readFile 回调中。因此,也要在回调中移动架构和模型创建。
  • 我是javascript 世界的新手。你能解释一下我该怎么做吗?

标签: json node.js mongoose


【解决方案1】:

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

因此,您需要推迟使用 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);

【讨论】:

  • 如果我在fs.readFile 内执行此操作,则在我启动节点服务器Schema hasn't been registered for model "Product" 时会显示错误。那么请告诉我如何在fs.readFile 方法之外定义Product 模型?
  • @AshikurRahman 如果您有这样复杂的启动依赖项,那么使用readFileSync 可能是最简单的。查看更新的答案。
  • 感谢您的出色解决方案。它终于工作了...... :)
猜你喜欢
  • 2012-01-20
  • 1970-01-01
  • 2015-05-28
  • 2018-12-11
  • 2017-01-14
  • 2015-02-04
  • 2016-06-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多