TypeError: writeSource is not a function

TypeError: writeSource is not a function

我正在使用 require 导入 3 个代码几乎相同的文件,但其中一个 (writeToSource.js) 不起作用。我以为我给函数提供了错误的参数,但是当我检查时一切都是正确的。我能做什么?我收到此错误:

TypeError: writeSource is not a function
    at Request._callback (/home/nika/Desktop/Girchi/Media-Girchi/assets/components/scrapOn.js:35:11) 

主 JavaScript 文件:

const request = require('request');
const cheerio = require('cheerio');
const writeDataToGirchi = require('./writeDataToGirchi');
const writeDataToImportants = require('./writeDataToImportants');
const writeToSource = require('./writeToSource');

function scrapOn(url, accept, accept1, sourceImgUrl) {
  try {
    request(url, (error, response, html) => {
      if (!error && response.statusCode == 200) {
        const $ = cheerio.load(html);

        const title = $('.article-title').text();
        const dataInfo = $('.date').first().text();
        const text = $('.article-body').text();
        const imgUrl = `https:${$('.global-figure-image  ').attr("src")}`;

        const writeGirchi = writeDataToGirchi("on.json", title, dataInfo, text, imgUrl, sourceImgUrl, url);
        const writeImportants = writeDataToImportants("on.json", title, dataInfo, text, imgUrl, sourceImgUrl, url);
        const writeSource = writeToSource("on.json", "On", title, dataInfo, text, imgUrl, sourceImgUrl, url);


        if (accept === "on" && accept1 === "on") {
          writeGirchi();
          writeImportants();
          writeSource();
        } else if (accept === "on") {
          writeImportants();
          writeSource();
        } else if (accept1 === "on") {
          writeGirchi();
          writeSource();
        } else {
          writeSource();
        }
      }
    });
  } catch (err) {
    console.log(err);
  }
}

writeToSource.js 文件:

const fs = require('fs');

function writeToSource(fileName, name, title, dataInfo, text, imgUrl, sourceImgUrl, url) {
  fs.readFile(`./assets/data/${fileName}`, (err, data) => {
    if (err) throw err;
    let newsData = JSON.parse(data);
    newsData[dataInfo] = {
      ...newsData[dataInfo],
      source: name,
      title: title,
      text: text,
      link: url,
      logo: sourceImgUrl,
      articleDate: dataInfo,
      imgUrl: imgUrl,
      important: false,
      fileName: fileName
    };
    newsData = JSON.stringify(newsData)
    fs.writeFileSync(`./assets/data/${fileName}`, newsData, (error) => {
      if (error) console.log(error)
    })
  });
}

module.exports = writeToSource;

提前致谢!

writeSource 确实不是一个函数,它是writeToSource(...args)的return值。如果你想让它成为一个函数,试试这个

const writeSource = () => { 
writeToSource("on.json", "On", title, dataInfo, text, imgUrl, sourceImgUrl, url);}

我现在觉得自己很傻。 我刚刚更换了 const writeSource = writeToSource() 带有箭头函数 - const writeSource = () => { writeToSource() }

而且有效。

您尚未导出函数 writeToSource 以使其在 main.js

中可用

module.exports = writeToSource;

创建了一个 codesandbox 只是为了阐明您将如何导出它https://codesandbox.io/s/currying-browser-5lzqy?file=/src/index.js

模拟你的文件并从主(index.js)调用 writeToSource 工作

我认为那是因为您没有导出函数或 WriteToSouce.js 文件中的任何内容

 export function WriteToSource(...args){
   //do your stuff
}

或者您可以使用

 module.exports=WriteToSource;

正如 require 的文档所述

The basic functionality of require is that it reads a JavaScript file, executes the file, and then proceeds to return the exports

在你的情况下,你从未导出函数,而是在主 js 文件中将其作为函数调用,这意味着 const writeToSource = require('./writeToSource'); returns 没有,writeToSource 未定义