为什么我在jade里找不到app.locals

Why can't I find app.locals in jade

我正在使用 express/node/jade 并为一些变量构建了一个模块。

我的 app.js app.locals.GVs = require('./utils/globalvars.js');

中有这一行

这是我的玉

extends ../layout

block content
  h1 Edit - Change Your Profile
  p There will hopefully be a form to change users here.
  form(id='form',action='')
    include ./includes/user-form.jade
    input(type='reset', value='Clear')
    input(type='submit', value='Update')
  script.
    //Wait for document to load.
    $(document).ready(function(){
      $('#firstname').val('#{data.firstname}');
      $('#lastname').val('#{data.lastname}');
      $('#username').val('#{data.username}');
      $('#phone').val('#{data.phone}');
      $('#email').val('#{data.email}');

      //Once form is submitted thisUserData (UserData) is created.
      $('#form').submit(function(){
        // creates the variable thisUserData as a new UserData.
        var thisUserData = new GVs.UserData($('#firstname').val(), $('#lastname').val(), $('#username').val(), $('#password').val(), $('#confirmpassword').val(), $('#phone').val(), $('#email').val());
        //POST call; Sends thisUserData and alerts that a user was updated.
        $.post('/user/edit', {userFormData: thisUserData}, function(data){
          alert("User updated!");
        });
      });
    });

调用 GV 时出现错误 var thisUserData = new GVs.UserData(...);

我也尝试用与 "data" 相同的方式传递它,但它做了同样的事情。

编辑:我试着看看我是否可以像这样在 jade 文件中获取 GV

p There will hopefully be a form to change users here.

变成了这个

p There will hopefully be a form to change users here. '#{GVs.UserData}'

就在那儿,为什么我能在翡翠中得到它而不是在脚本中得到它?

你需要在客户端JS和服务器端之间传递变量,因为客户端不知道node.js全局变量。例如:

extends ../layout

block content
  h1 Edit - Change Your Profile
  p There will hopefully be a form to change users here.
  form(id='form',action='')
    include ./includes/user-form.jade
    input(type='reset', value='Clear')
    input(type='submit', value='Update')
  script.
    var GVs = !{JSON.stringify(GVs)};
    //Wait for document to load.
    $(document).ready(function(){
      $('#firstname').val('#{data.firstname}');
      $('#lastname').val('#{data.lastname}');
      $('#username').val('#{data.username}');
      $('#phone').val('#{data.phone}');
      $('#email').val('#{data.email}');

      //Once form is submitted thisUserData (UserData) is created.
      $('#form').submit(function(){
        // creates the variable thisUserData as a new UserData.
        var thisUserData = new GVs.UserData($('#firstname').val(), $('#lastname').val(), $('#username').val(), $('#password').val(), $('#confirmpassword').val(), $('#phone').val(), $('#email').val());
        //POST call; Sends thisUserData and alerts that a user was updated.
        $.post('/user/edit', {userFormData: thisUserData}, function(data){
          alert("User updated!");
        });
      });
    });