未在车把猫鼬中定义的字段

时间:2018-07-02 13:54:52

标签: node.js mongodb express mongoose handlebars.js

  

我的模型(此模型中没有'test_field')

getAppModel(){
        var AppSchema = mongoose.Schema({
            name : {
                type : String,
                required : true
            },
            overview : {
                type : String,
                required: true
            },
            team : {
                type : String,
                required: true
            },
            display_name : {
                type : String,
                required: true,
            },
            organization : {
                type : String,
                required: true,
            },
            created_on:{
                type : Date,
                default: Date()
            },
            url : {
                type : String,
                required: false,
            }
        }, { strict: false,"versionKey": false });
        try{
            var AppModel = mongoose.model(this.appCollection);
        }catch(error){
            var AppModel = mongoose.model(this.appCollection, AppSchema, this.appCollection);
        }
        return AppModel;
    }
  

我的路线

router.get('/dashboard', checklogin, function(req, res){
    //res.sendFile("dashboard.html", {"root": __dirname.replace('routes','views')});
    var appControllerObj = new appController();
    appControllerObj.getAllApps()
    .then(function(result){
        var template_data = {apps: result};
        console.log(template_data);

//在这里,我正在记录template_data,问题末尾在下面给出了其日志

        var views_path = __dirname.replace('routes','views');
        var inFile = views_path + '/dashboard.hbs';
        var outFile = views_path + '/dashboard.html';
        var source = fs.readFileSync(inFile, 'utf8');
        var template = handlebars.compile(source, { strict: true });
        var html = template(template_data);
        fs.writeFileSync(outFile, html);
        res.sendFile("dashboard.html", {"root": __dirname.replace('routes','views')});
    }).catch(function(err){
        console.log(err);
        res.send(err);
    });
});
  

dashboard.hbs(视图)

<div class="row">
        {{#each apps}}
        <div class="col-md-6 bg-primary text-center full-height urlshortner">
            <h1>{{display_name}}</h1>
            <h3>Overview</h3>
            <p>{{overview}}</p>
            <ol>
                {{#if url}}
                    <li><a href="http://{{url}}" target="_blank">Application Front-End</a></li>
                {{/if}}
                <li>API playground</li>
                <li>Get Your API key <button>Generate</button></li>
                <li>{{team}}</li>
                <li>{{organization}}</li>
                <li>{{test_field}}</li><!-- this field is not showing up until i add it in model -->
            </ol>
        </div>
        {{/each}}
    </div>

这是我得到的例外情况

{ Error: "test_field" not defined in { created_on: 2018-07-02T10:44:25.000Z,
  _id: 5b3a02261570331ebf5fd964,
  overview: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.',
  team: 'Product Development',
  display_name: 'Survey Blaster',
  organization: 'Ibex.digital',
  test_field: 'Value of test field' }
    at strict (/var/www/html/app-center/node_modules/handlebars/dist/cjs/handlebars/runtime.js:96:15)
    at eval (eval at createFunctionContext (/var/www/html/app-center/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:16:48)
    at prog (/var/www/html/app-center/node_modules/handlebars/dist/cjs/handlebars/runtime.js:219:12)
    at execIteration (/var/www/html/app-center/node_modules/handlebars/dist/cjs/handlebars/helpers/each.js:51:19)
    at Object.<anonymous> (/var/www/html/app-center/node_modules/handlebars/dist/cjs/handlebars/helpers/each.js:61:13)
    at Object.eval [as main] (eval at createFunctionContext (/var/www/html/app-center/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:6:31)
    at main (/var/www/html/app-center/node_modules/handlebars/dist/cjs/handlebars/runtime.js:173:32)
    at ret (/var/www/html/app-center/node_modules/handlebars/dist/cjs/handlebars/runtime.js:176:12)
    at ret (/var/www/html/app-center/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:525:21)
    at /var/www/html/app-center/routes/user.js:23:14
    at process._tickCallback (internal/process/next_tick.js:68:7)
  description: undefined,
  fileName: undefined,
  lineNumber: undefined,
  message: '"test_field" not defined in { created_on: 2018-07-02T10:44:25.000Z,\n  _id: 5b3a02261570331ebf5fd964,\n  overview: \'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\\\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\',\n  team: \'Product Development\',\n  display_name: \'Survey Blaster\',\n  organization: \'Ibex.digital\',\n  test_field: \'Value of test field\' }',
  name: 'Error',
  number: undefined }
  

问题:模型定义中没有的任何字段都不会   通过手柄栏进行编译,因此不会显示在视图中。

     

当我在猫鼬模型中添加字段时,它工作得很好。

记录template_data(无论我是否在模型中添加数据,test_field始终在结果对象中)

假设:当字段位于结果对象中时,应由车把进行编译。它与猫鼬模型有什么关系?

{ apps:     [ { created_on: 2018-07-02T10:44:25.000Z,
       _id: 5b3a02261570331ebf5fd964,
       overview: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.',
       team: 'Product Development',
       display_name: 'Survey Blaster',
       organization: 'Ibex.digital',
       test_field: 'Value of test field' },
     { created_on: 2018-07-02T10:44:25.000Z,
       _id: 5b3a023f1570331ebf5fd965,
       name: 'Url Shortner',
       overview: 'URL shortening is a technique on the World Wide Web in which a Uniform Resource Locator (URL) may be made substantially shorter and still direct to the required page. This is achieved by using a redirect which links to the web page that has a long URL. For example, the URL "http://example.com/assets/category_B/subcategory_C/Foo/" can be shortened to "https://example.com/Foo". Often the redirect domain name is shorter than the original one. A friendly URL may be desired for messaging technologies that limit the number of characters in a message (for example SMS), for reducing the amount of typing required if the reader is copying a URL from a print source, for making it easier for a person to remember',
       team: 'Product Development',
       display_name: 'Url Shortner',
       organization: 'Ibex.digital',
       url: 'ibex.sh',
       test_field: 'Value of test field' } ] }

0 个答案:

没有答案
相关问题