繁体   English   中英

node.js中的反引号

[英]Backticks in node.js

我正在尝试将以下代码用作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);

我得到以下结果,但我不明白为什么。

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

我唯一看到的是可能未正确处理模板字符串,导致未定义变量xul

正如@Andreas提到的那样,您有错字应该是console.log('value is: ', xu1);

而且即使您运行得到xu1 is: http://api.openweathermap.org/data/2.5.weather?id=$(id)&APPID=$(apiKey)

那是因为您必须使用$ {}而不是$()。 所以最后的解决方法是

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