简体   繁体   English

在Jade脚本中未定义NodeJS和Jade / Pug变量?

[英]NodeJS & Jade/Pug variables not defined in jade script?

When I pass a variable over to jade, it looks like it works correctly, for example #{myvar} works but, when I try to call it in a script block in Jade, it doesn't load at all. 当我将变量传递给jade时,它看起来可以正常工作,例如#{myvar}可以工作,但是当我尝试在Jade中的脚本块中调用它时,它根本不会加载。 Upon doing a typeof(myvar) in my script block it said that it was undefined at one point but now it doesn't return anything. 在我的脚本块中执行typeof(myvar)后,它说它在某一时刻未定义,但现在不返回任何内容。 My code looks something like this: 我的代码如下所示:

- typeof(rm120[0].description) <---- Returns string
div#change !{rm120[0].description} <----- Works perfectly fine
div#test 

script.
  change.innerHTML = "test code" <------ Works perfectly fine
  test.innerHTML = rm120[0].description 
  test.innerHTML = typeof(rm120)
  test.innerHTML = typeof(locals.rm120)

all the lines after the first one in script. 脚本中第一行之后的所有行。 seems to never load or just get ignored and I'm extremely confused as to why I can't use the objects I passed through from my node.js file in my script block. 似乎永远不会加载或被忽略,我对于为什么不能在脚本块中使用从node.js文件传递的对象感到非常困惑。 Any help would be much appreciated! 任何帮助将非常感激!

The script tag in Pug renders text which is sent to the client to run as client side Javascript. Pug中的script标签呈现文本,该文本发送到客户端以作为客户端Javascript运行。 As server side variables don't exist on the client, they need to be passed across to the client somehow. 由于服务器端变量在客户端上不存在,因此需要以某种方式将它们传递给客户端。

Two possible ways are rendering the variables in Pug or passing the data out via an AJAX call. 两种可能的方法是在Pug中呈现变量或通过AJAX调用传递数据。

Render 给予

Rendering variables into the Javascript is the simplest method to pass data but I tend to keep this limited to very simple, bootstrap data required for the JS App. 将变量呈现到Javascript中是传递数据的最简单方法,但我倾向于将其限制为JS App所需的非常简单的引导数据。

script.
   let rm120 = !{JSON.stringify(rm120)}
   console.log(rm210)

AJAX AJAX

Using an API endpoint to pass JSON objects out is more complicated to setup as there needs to be a server endpoint and you start dealing with async requests. 使用API​​终结点将JSON对象传递出去的设置更加复杂,因为需要一个服务器终结点,并且您开始处理异步请求。 As an app starts passing more data, or more complex data, then it's a more scalable method of sharing. 当应用开始传递更多数据或更复杂的数据时,这是一种更具扩展性的共享方法。

script.
   fetch("/api/v1/rm120")
     .then(response => response.blob())
     .then(body => {
        console.log(body)
     })
     .catch(err => console.log(err))

I assume you're trying to insert the actual value of rm120[0].description on your html file, which you want to run as a script once it's delivered to client side. 我假设您正在尝试在html文件中插入rm120[0].description的实际 ,一旦将其交付给客户端,您就希望将其作为脚本运行。 In which case you need to use #{} or !{}. 在这种情况下,您需要使用#{}或!{}。

If you don't, then the word "rm120[0].description" will be written onto your html file, quite literally. 如果您不这样做,那么“ rm120 [0] .description”一词将按字面意思写到您的html文件中。 And when it runs client side, there won't be an rm120 presumably. 当它在客户端运行时,大概不会有RM120。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM