简体   繁体   English

node.js中的反引号

[英]Backticks in node.js

I am attempting to use the following code as a file test2.js with node.js version 12.0. 我正在尝试将以下代码用作node.js版本12.0的文件test2.js

let apiKey = '117fa18eaf7312fa52f593c6d52fc48d';
let id = '6094817';
let xu1 = `http://api.openweathermap.org/data/2.5.weather?id=$(id)&APPID=$(apiKey)`;
console.log('xul is: ', xul);

I get the following results and I am unable to understand why. 我得到以下结果,但我不明白为什么。

jgossage@jgossage-XPS-8700:/LinuxData/Projects/node-weather$ node test2.js
/LinuxData/Projects/node-weather/test2.js:4
console.log('value is: ', xul);
                          ^

ReferenceError: xul is not defined
    at Object.<anonymous> (/LinuxData/Projects/node-weather/test2.js:4:27)
    at Module._compile (internal/modules/cjs/loader.js:936:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
    at Module.load (internal/modules/cjs/loader.js:790:32)
    at Function.Module._load (internal/modules/cjs/loader.js:703:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
    at internal/main/run_main_module.js:17:11

The only thing I can see is that possibly the template string is not being handled properly, resulting in the variable xul being not defined. 我唯一看到的是可能未正确处理模板字符串,导致未定义变量xul

As @Andreas mentioned you have a typo it should be console.log('value is: ', xu1); 正如@Andreas提到的那样,您有错字应该是console.log('value is: ', xu1);

but also even if you run that you get xu1 is: http://api.openweathermap.org/data/2.5.weather?id=$(id)&APPID=$(apiKey) 而且即使您运行得到xu1 is: http://api.openweathermap.org/data/2.5.weather?id=$(id)&APPID=$(apiKey)

That is because you have to use ${} instead of $(). 那是因为您必须使用$ {}而不是$()。 So the final fix is 所以最后的解决方法是

let xu1 = `http://api.openweathermap.org/data/2.5.weather?id=${id}&APPID=${apiKey}`;

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

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