Javascript API密钥未定义?
错误如下:Javascript API密钥未定义?,javascript,Javascript,错误如下: /home/runner/CrowdedMajesticRouter/index.js:3 let apiKey,cJhlPVSd5-wzJ0iOv; ^ SyntaxError: Unexpected token '-' let apiKey = cJhlPVSd5-wzJ0iOv; let search = (term, location) => { return fetch(`https://cors-anywhere.hero
/home/runner/CrowdedMajesticRouter/index.js:3
let apiKey,cJhlPVSd5-wzJ0iOv;
^
SyntaxError: Unexpected token '-'
let apiKey = cJhlPVSd5-wzJ0iOv;
let search = (term, location) => {
return fetch(`https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/search?term=${term}&location=${location}`, {
headers: {
Authorization: `Bearer ${apiKey}`
}
}).then(response => {
return response.json();
}).then(jsonResponse => {
if (jsonResponse.businesses) {
console.log(jsonResponse.businesses)
}
});
}
search('Mexican', 'Los Angeles')
如果按照以下方式更正代码:
/home/runner/CrowdedMajesticRouter/index.js:3
let apiKey,cJhlPVSd5-wzJ0iOv;
^
SyntaxError: Unexpected token '-'
let apiKey = cJhlPVSd5-wzJ0iOv;
let search = (term, location) => {
return fetch(`https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/search?term=${term}&location=${location}`, {
headers: {
Authorization: `Bearer ${apiKey}`
}
}).then(response => {
return response.json();
}).then(jsonResponse => {
if (jsonResponse.businesses) {
console.log(jsonResponse.businesses)
}
});
}
search('Mexican', 'Los Angeles')
那么错误是:
ReferenceError: cJhlPVSd5 is not defined
at /home/runner/CrowdedMajesticRouter/index.js:3:14
at Script.runInContext (vm.js:131:20)
at Object.<anonymous> (/run_dir/interp.js:156:20)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
at Module.load (internal/modules/cjs/loader.js:977:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js
ReferenceError:未定义cJhlPVSd5
at/home/runner/crowdedmajestcrouter/index.js:3:14
位于Script.runInContext(vm.js:131:20)
反对。(/run_dir/interp.js:156:20)
at模块编译(内部/modules/cjs/loader.js:1133:30)
at Object.Module._extensions..js(internal/modules/cjs/loader.js:1153:10)
at Module.load(内部/modules/cjs/loader.js:977:32)
at Function.Module._load(内部/modules/cjs/loader.js:877:14)
在Function.executeUserEntryPoint[作为runMain](internal/modules/run_main.js:74:12)
在internal/main/run_main_module.js中
每叫声:/business/搜索
此端点根据提供的搜索条件返回多达1000家企业。它有一些关于业务的基本信息。要获取详细信息和评论,请使用此处返回的业务ID,并参考/businesss/{ID}和/businesss/{ID}/reviews端点
注意:此时,API不会在没有任何审查的情况下返回业务。从您发布的代码中,我们可以看到您没有声明变量
let apiKey
somekey;
应该是
let apiKey, somekey;
let apiKey = 'cJhlPVSd5-wzJ0iOv';
或
这就是为什么会出现ReferenceError
@编辑
现在我明白了
let apiKey,
cJhlPVSd5-wzJ0iOv;
应该是
let apiKey, somekey;
let apiKey = 'cJhlPVSd5-wzJ0iOv';
不知道你为什么要用逗号把它分开 密钥似乎不被接受,因为在密钥之间添加了连字符。密钥的格式为:12314-zx81,但我收到一个'''SyntaxError:Unexpected token'-'''按原样粘贴整个代码,只需将密钥替换为其他字符串即可。当你仅仅删掉一部分代码并用
替换它时,很难做到精确。我认为声明字符串的正确方法是让apiKey='cJhlPVSd5-wzJ0iOv'代码>。使用使apiKey=cJhlPVSd5-wzJ0iOv代码>代码询问变量cJhlPVSd5
和变量wzJ0iOv
之间的差异,两者都不存在。@BurningKarl你说得对,忘记引号了^。我要编辑。