【问题标题】:please how can l add a runtime option to disable the check or this warning请问如何添加运行时选项来禁用检查或此警告
【发布时间】:2020-05-10 02:55:51
【问题描述】:

我正在创建一个联系表单,它将在屏幕上显示一些消息,让用户知道该表单已成功提交 但是,我总是在我的 git 终端中收到错误消息。以下是错误信息。

Handlebars:访问已被拒绝解析属性“消息”,因为它不是其父级的“自己的属性”。 您可以添加运行时选项来禁用检查或此警告: 详情见https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access

下面是server.js和message.handlebars代码;

app.post('/contactUs',function(req,res){
    console.log(req.body);
    //res.send('thanks');
    const newMessage = {
        fullname: req.body.fullname,
        email: req.body.email,
        message: req.body.message,
        date: new Date()
    }


    new Message(newMessage).save(function(err, message){
        if (err){
            throw err;
        }else{
            Message.find({}).then(function(messages){
                if(messages){                    
                    res.render('newmessage',{
                        title: 'Sent',
                        messages:messages
                    });
                }else{
                    res.render('noMessage',{
                        title: 'Not found'
                    });
                }
            });
        }

    });

});


<h1>Thank you for contacting</h1>
{{#each messages}}
<p>{{fullname}}</p>
<p>{{email}}</p>
<p>{{message}}</p>
<small>{{date}}</small>
<hr>
{{else}}
<p>No messages</p>
{{/each}}
<a href="/contact"><button class="btn btn-success">Contact Again</button></a>

【问题讨论】:

    标签: runtime-error handlebars.js


    【解决方案1】:

    我猜你正在做与我正在研究的相同的约会应用教程。我通过以下给定的链接解决了这个问题:https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access 然后我下载了包 npm install @handlebars/allow-prototype-access 然后在 server.js 中添加:

    const express = require('express');
    const Handlebars = require('handlebars')
    const expressHandlebars = require('express-handlebars');
    const {allowInsecurePrototypeAccess} = require('@handlebars/allow-prototype-access')
    

    在 server.js 中向下滚动到 app.engine:

        // setup view engine
    app.engine('handlebars', exphbs({
      defaultLayout: 'main',
    
    }));
    app.set('view engine', 'handlebars');
    

    然后添加:handlebars: allowInsecurePrototypeAccess(Handlebars) 它应该是这样的:

    // setup view engine
    app.engine('handlebars', exphbs({
      defaultLayout: 'main',
       handlebars: allowInsecurePrototypeAccess(Handlebars)
    }));
    app.set('view engine', 'handlebars');
    

    我希望这会有所帮助。祝你好运。

    【讨论】:

      【解决方案2】:

      这可能是由于最新版本的 Handlebars 造成的,但这可以通过向 Handlebars 添加依赖项来解决。

      npm i -D handlebars@4.5.0

      【讨论】:

        【解决方案3】:

        只需使用与教程中显示的完全相同版本的“express-handlebars”即可。最新版本的车把导致了这个问题。 至少这样做,对我有用。

        【讨论】:

          【解决方案4】:

          这对我有用,与第一个答案非常相似:

          const express = require('express');
          const handlebars = require('handlebars')
          const expressHandlebars = require('express-handlebars');
          const { allowInsecurePrototypeAccess } = require('@handlebars/allow-prototype-access')
          
          // configuration express for handlebars
          app.engine('hbs', expressHandlebars({ extname: 'hbs', 
            defaultLayout: 'mainLayout', 
            handlebars: allowInsecurePrototypeAccess(handlebars), 
            layoutsDir: __dirname + '/views/layouts/' })
          ); 
          

          【讨论】:

            【解决方案5】:
            const Handlebars=require('handlebars')
            const {allowInsecurePrototypeAccess} = require('@handlebars/allow-prototype-access')
            app.engine('handlebars',exphbs({defaultLayout:'main',
            handlebars: allowInsecurePrototypeAccess(Handlebars)
            }));
            app.set('view engine','handlebars');
            

            这发生在新版本的 express-handlebars 模块上。要解决此问题,请将以下运行时选项传递到车把引擎配置中。 首先,在终端中输入以下命令并回车:

            npm install @handlebars/allow-prototype-access
            

            和

            npm install handlebars
            

            然后将这些代码写入您的 server.js 文件中

            const Handlebars = require('handlebars')
            

            和

            const {allowInsecurePrototypeAccess} = require('@handlebars/allow-prototype-access')
            

            然后添加这一行:

            handlebars: allowInsecurePrototypeAccess(Handlebars)
            

            在您的设置视图引擎中。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-11-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-02-04
              • 2018-08-20
              • 2011-08-01
              相关资源
              最近更新 更多