我试图使用node-fetch
来获取这个包含这个JSON文件的网站,在那里我可以将它用于我的异议机器人。
法典(JS):
const fetch = import("node-fetch")
// some code until
data = ""
try {
data = await fetch(`http://meme-api.herokuapp.com/gimme/${subreddit.toLowerCase()})}`).then(res => res.json)
errored = false
} catch (error) {
throw error;
}
它错误地说:
TypeError: fetch is not a function
我该怎么解决这个问题?
FYI使用require()会导致这样的错误:
const fetch = require("node-fetch")
^
Error [ERR_REQUIRE_ESM]: require() of ES Module F:\Users\Basil Atif\Folders\VsauceBot\node_modules\node-fetch\src\index.js from F:\Users\Basil Atif\Folders\VsauceBot\src\Commands\reddit.js not supported.
Instead change the require of index.js in F:\Users\Basil Atif\Folders\VsauceBot\src\Commands\reddit.js to a dynamic import() which is available in all CommonJS modules.
code: 'ERR_REQUIRE_ESM'
}
发布于 2021-09-27 19:33:08
尝试使用这种类型的导入:
import fetch from 'node-fetch';
发布于 2022-11-15 08:41:42
错误:
let get=document.getElementById("get");
let **fetch**=document.getElementById("fetch"); //this var create problem
let content=document.getElementById("content");
function getData(){
let url= "https://api.github.com/users/github";
fetch(url).then(
(res)=>{
return res.text();
}).then((data)=>{
content.innerHTML = data;
});
}
getData()
输出: 错误
解决方案:
let get=document.getElementById("get");
let **fet**=document.getElementById("fetch");// change the name of the variable
let content=document.getElementById("content");
function getData(){
let url= "https://api.github.com/users/github";
fetch(url).then(
(res)=>{
return res.text();
}).then((data)=>{
content.innerHTML = data;
});
}
getData()
输出: 现在数据被取了
https://stackoverflow.com/questions/69352254
复制相似问题