Javascript 如何修复';this.reduce()不是函数';?
这是我的代码:Javascript 如何修复';this.reduce()不是函数';?,javascript,node.js,express,Javascript,Node.js,Express,这是我的代码: app.get(“/”,(req,res)=>{ 常量减速机=(acc,GuidCount)=>acc+GuidCount; const results=client.shard.fetchClientValues('guilds.cache.size'); console.log(结果) 让guildCount=results.reduce(reducer,0) console.log(guildCount) renderTemplate(res,req,“index.ejs”
app.get(“/”,(req,res)=>{
常量减速机=(acc,GuidCount)=>acc+GuidCount;
const results=client.shard.fetchClientValues('guilds.cache.size');
console.log(结果)
让guildCount=results.reduce(reducer,0)
console.log(guildCount)
renderTemplate(res,req,“index.ejs”,{guildCount});
});
一些细节:`
client.shard.fetchClientValues('guilds.cache.size')
returnPromise{}
当我要加载以下内容时出错:
TypeError:results.reduce不是一个函数
``
此代码可用于:
client.shard.fetchClientValues('guilds.cache.size'))
。然后(结果=>{
log(`${results.reduce((acc,GuidCount)=>acc+GuidCount,0)}总工会`);
})
.catch(控制台错误);
但是我必须在变量中定义结果才能导出它
我如何解决这个问题?有没有其他方法可以用reduce定义结果?您的第二种方法是正确的。从这一点开始,您的方法必须是异步的。一旦异步,就始终异步。没有办法强迫它回到同步状态 幸运的是,您使用的
.get()
方法已经是异步的,因此只需将之后的所有内容移到中即可
你的第二种方法是正确的。从这一点开始,您的方法必须是异步的。一旦异步,就始终异步。没有办法强迫它回到同步状态
幸运的是,您使用的.get()
方法已经是异步的,因此只需将之后的所有内容移到中即可
Reduce方法适用于数组,所以如果Reduce是未定义的,您首先知道的“结果”不是数组。现在的问题是为什么
const results = client.shard.fetchClientValues('guilds.cache.size');
是返回承诺的异步方法。当您在没有的情况下使用它时。然后()
您将得到一个promise对象作为回报,而不是数组。你必须等待承诺得到解决
const results = await client.shard.fetchClientValues('guilds.cache.size');
要解决此问题,您可以使用异步等待
app.get("/", async (req, res) => {
const reducer = (acc, guildCount) => acc + guildCount;
const results = await client.shard.fetchClientValues('guilds.cache.size');
console.log(results)
let guildCount = results.reduce(reducer, 0)
console.log(guildCount)
renderTemplate(res, req, "index.ejs", { guildCount });
});
欲了解更多关于
而且Reduce方法对数组有效,所以如果Reduce是未定义的,您首先知道的“结果”不是数组。现在的问题是为什么
const results = client.shard.fetchClientValues('guilds.cache.size');
是返回承诺的异步方法。当您在没有的情况下使用它时。然后()
您将得到一个promise对象作为回报,而不是数组。你必须等待承诺得到解决
const results = await client.shard.fetchClientValues('guilds.cache.size');
要解决此问题,您可以使用异步等待
app.get("/", async (req, res) => {
const reducer = (acc, guildCount) => acc + guildCount;
const results = await client.shard.fetchClientValues('guilds.cache.size');
console.log(results)
let guildCount = results.reduce(reducer, 0)
console.log(guildCount)
renderTemplate(res, req, "index.ejs", { guildCount });
});
欲了解更多关于
reduce
是数组助手,其中result
是promise
因此,要么在中执行操作,然后像
client.shard.fetchClientValues('guilds.cache.size').then(results => results.reduce(reducer, 0))
或
使用async
wait
const start = async () => {
...
const results = await client.shard.fetchClientValues('guilds.cache.size')
results.reduce(reducer, 0)
...
}
start()
reduce
是数组助手,其中result
是promise
因此,要么在中执行操作,然后像
client.shard.fetchClientValues('guilds.cache.size').then(results => results.reduce(reducer, 0))
或
使用async
wait
const start = async () => {
...
const results = await client.shard.fetchClientValues('guilds.cache.size')
results.reduce(reducer, 0)
...
}
start()
当承诺解析中的结果是什么格式时?当承诺解析中的结果是什么格式时?您的catch()
还应至少以500错误响应请求您的catch()
还应至少以500错误响应请求