小编R. *_*lie的帖子

将 import() 转换为同步

我正在尝试将所有 noderequire()转换为import()语句,但是,这些是异步的,我遇到了一些麻烦。

现在我有:

import * as fs from 'fs';

const paths = fs.readdirSync('./src/modules').map(path => './modules/' + path.slice(0, path.length - 3));

const classes = [];

paths.forEach(path => {
    let bClass = require(path);

    try {
        classes.push(new bClass.default());
    }
    catch (err) {
        //Here for if no default import
    }
});
Run Code Online (Sandbox Code Playgroud)

并希望将该require(path)部分转换为一个import()但仍希望保持同步,这可能吗?如果是,我会怎么处理?

编辑:多一点上下文。我有一个我们想要导入的模块列表,我们正在这样做,所以如果某个模块出现问题,我们可以将其注释掉/删除它,而不必重新编码其他所有内容。我只需要动态同步导入而不使用require().

javascript node.js typescript

15
推荐指数
5
解决办法
7966
查看次数

找出某人是否有角色

我为服务器制作了一个简单的引用机器人,但管理员只希望mod +人能够添加引号以避免垃圾邮件.我去了文档并做了一切,但我不能让它工作.这就是我所拥有的:

//other code
else if (command === "addquote" && arg) {
    let adminRole = message.guild.roles.find("name", "Admin");
    let modRole = message.guild.roles.find("name", "Mod");

    if(message.member.roles.has(adminRole) || message.member.roles.has(modRole)){
        const hasArr = arr.some((el) => {
            return el.toLowerCase().replace(/\s/g, '') === arg.toLowerCase().replace(/\s/g, '');
        });

        if(hasArr){
            message.channel.send(arg.replace(/\s+/g,' ').trim() + " is already a Quote");
        } else {
            fs.appendFileSync('./Quotes.txt', '\r\n' + arg);
            message.channel.send("Quote added: " + arg);
            arr.push(arg);            
        }   
    }
}
Run Code Online (Sandbox Code Playgroud)

这非常挑剔.有时,如果用户具有mod角色,它将起作用,大多数时候它不会.如果我做

console.log(message.memeber.roles.has(adminRole));
console.log(message.memeber.roles.has(modRole));
Run Code Online (Sandbox Code Playgroud)

两者都输出为假,但会起作用吗?老实说,我现在还不知道.

javascript roles node.js discord.js

7
推荐指数
5
解决办法
3万
查看次数

作为道具传递与提取缓存 Apollo 客户端 Nextjs

我在apollo-client repo 中发布了这个,但我想我也会问 stackoverflow

你好!我是阿波罗(以及整个 graphql)的新手,并且对 SSR / SSG 有一些疑问。我从概念上知道 SSR/SSG 是什么以及它是如何工作的,但对 apollo-client 不太了解。

我已经尝试在网上搜索和搜索正确执行此操作的方法,并且看到了两个版本而几乎没有解释原因,所以我这篇文章的目标是有一个指向和去的地方“这就是为什么你做一个其他”。

做之间有什么好处/坏处

这是 TypeScript 和伪代码的混合体,请不要批评语法 kthx

// apolloClient.ts
const client = new ApolloClient({
  link: new HttpLink({ uri: '/graphql' }),
  cache: new InMemoryCache(),
});
Run Code Online (Sandbox Code Playgroud)
// component.tsx
import client from '../apolloClient';

const Component: FunctionalComponent = ({ data }) => {
   ...
}

export const getServerSideProps: GetServerSideProps = async () => {
  const { data } = client.query(...);

  return {
    props: { data }
  }
} …
Run Code Online (Sandbox Code Playgroud)

typescript server-side-rendering next.js apollo-client

6
推荐指数
1
解决办法
174
查看次数