NodeJS 无法获取数据:fetch未定义=>定义时,TypeError:fetch不是函数

w46czmvw  于 2022-12-03  发布在  Node.js
关注(0)|答案(1)|浏览(265)

我正在创建一个生成器,使用您的谷歌catchall域生成一个电子邮件帐户列表,我是如何去做的,是一个函数将生成一个随机的名字和姓氏从一个数组,并合并在一起的catchall域。本质上,结果将是fname + lname + domain = johndoe@domain.com,但由于某种原因,我得到了一个错误。终端说:“Fetch是没有定义的,”但是当我通过node-fetch包(const fetch = require('node-fetch');,然后它说“fetch不是一个函数”。我试图使用内置的Fetch API来获取数据,因为我基于的脚本指示这样做,在终端说它没有定义之后,我尝试使用节点获取包来定义变量fetch,希望它能修复它,但也没有运气。有人能解释为什么fetch不是一个函数,而且fetch没有定义吗?

const prompt = require("prompt-sync") ({sigint: true });
const fs = require("fs").promises;
const request = require('request');
// const fetch = require('node-fetch');
const random_useragent = require('random-useragent');
const { Webhook, MessageBuilder } = require('discord-webhook-node');

const StealthPlugin = require('puppeteer-extra-plugin-stealth');
puppeteer.use(StealthPlugin());

( async () => {

    const browser = await puppeteer.launch({ 
        headless: false,
        executablePath: `/Applications/Google Chrome.app/Contents/MacOS/Google Chrome`,
        userDataDir: `/Users/bran_d0_n/Library/Application Support/Google/Chrome/Default`,
        ignoreHTTPSErrors: true,
        ignoreDefaultArgs: ['--enable-automation'],
        args: [
                `--disable-blink-features=AutomationControlled`,
                `--enable-blink-feautres=IdleDetection`,
                `--window-size=1920,1080`,
                `--disable-features=IsolateOrigins,site-per-process`, 
                `--blink-settings=imagesEnabled=true`
        ]
    });

    //------------------ Random Password Generator Function ------------------//

    function generatePassword() {

    let pass = '';
    let str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 
        'abcdefghijklmnopqrstuvwxyz0123456789@#$';

        for ( let i = 1; i <= 8; i++) {
            var char = Math.floor(Math.random() * str.length + 1);
            pass += str.charAt(char)
        }
            return pass;
    }

    //------------------ First & Last Name Generator Function ------------------//
    
    async function fetchData(url) {
        const response = await fetch(url);
        return response.json();
    }

    async function fetchData(url) {
        try {
            const response = await fetch(url);
            if (!response.ok) {
                throw new Error('Network Response Invalid');
            }
            return response.json();
        } catch (error) {
            console.error('Unable To Fetch Data:', error)
        }
    }

    function fetchNames(nameType) {
        return fetchData(`https://www.randomlists.com/data/names-${nameType}.json`);
    }

    function pickRandom(list) {
        return list[Math.floor(Math.random() * list.length)];
    }
   async function generateName(gender) {
        try {
            const response = await Promise.all ([
                fetchNames(gender || pickRandom(['male', 'female'])),
                fetchNames('surnames')
            ]);

            const [ firstNames, lastNames] = response;

            const firstName = pickRandom(firstNames.data);
            const lastName = pickRandom(lastNames.data);

            return `${firstName} ${lastName}`;
        } catch (error) {
            console.error('Unable To Generate Name:', error);
        }
    }

        console.log('Loading Browser...');


    // Account Values
        var bDayval = '01/05/22' + (Math.floor((Math.random() * ( 99-55 )) + 55 )).toString();
        var passwordVal = generatePassword();
        var fnameVal = generateName();
        var lnameVal = generateName();
        var info;
        var themessage;
        var phoneNum;
        var userpass;
hivapdat

hivapdat1#

加载和配置模块
v3中node-fetch是一个仅限ESM的模块-您无法使用require()导入它。
如果您无法切换到ESM,请使用与CommonJS保持兼容的v2。v2的关键错误修复将继续发布。
您应该使用

import fetch from 'node-fetch';

(请记住将"type": "module"添加到package.json
或安装旧版本

npm install node-fetch@2

相关问题