关键词:
【中文标题】TypeError:x 不是 Node.js 中的函数【英文标题】:TypeError: x is not a function in Node.js 【发布时间】:2018-11-26 13:37:24 【问题描述】:我正在开发一个 Electron 应用程序,我的目标是“拆分”index.js
(主进程)文件。目前,我已将与菜单栏相关和与触控栏相关的代码放入两个单独的文件中,menu.js
和 touchBar.js
。这两个文件都依赖于一个名为redir
的函数,该函数位于index.js
中。每当我尝试在我的菜单栏中激活 click
事件时 - 它依赖于 redir
- 我都会收到错误消息:
TypeError: redir is not a function
。这也适用于我的 Touch Bar 代码。
这是我的(截断的)文件:
index.js
const app, BrowserWindow = require('electron'); // eslint-disable-line
const initTB = require('./touchBar.js');
const initMenu = require('./menu.js');
...
let mainWindow; // eslint-disable-line
// Routing + IPC
const redir = (route) =>
if (mainWindow.webContents)
mainWindow.webContents.send('redir', route);
;
module.exports.redir = redir;
function createWindow()
mainWindow = new BrowserWindow(
height: 600,
width: 800,
title: 'Braindead',
titleBarStyle: 'hiddenInset',
show: false,
resizable: false,
maximizable: false,
);
mainWindow.loadURL(winURL);
initMenu();
mainWindow.setTouchBar(initTB);
...
app.on('ready', createWindow);
...
menu.js
const redir = require('./index');
const app, Menu, shell = require('electron'); // eslint-disable-line
// Generate template
function getMenuTemplate()
const template = [
...
label: 'Help',
role: 'help',
submenu: [
label: 'Learn more about x',
click: () =>
shell.openExternal('x'); // these DO work.
,
,
...
],
,
];
if (process.platform === 'darwin')
template.unshift(
label: 'Braindead',
submenu: [
...
label: 'Preferences...',
accelerator: 'Cmd+,',
click: () =>
redir('/preferences'); // this does NOT work
,
...
],
);
...
;
return template;
// Set the menu
module.exports = function initMenu()
const menu = Menu.buildFromTemplate(getMenuTemplate());
Menu.setApplicationMenu(menu);
;
我的文件结构很简单——三个文件都在同一个目录中。
也欢迎任何代码批评;我花了好几个小时努力想弄清楚这一切。
【问题讨论】:
【参考方案1】:redir
它不是一个函数,因为您正在导出一个对象,其中包含一个 redir
属性,它是一个函数。
所以你应该使用:
const redir = require('./index.js');
或者这样导出
module.exports = redir
当你这样做时:module.exports.redir = redir;
您正在导出: redir: [Function]
【讨论】:
【参考方案2】:您正在导出
module.exports.redir = redir;
这意味着你的导入
const redir = require('./index');
是导出的对象。 redir
恰好是它的关键之一。要使用该功能,请使用
const redir = require('./index').redir;
或者直接解构成redir
const redir = require('./index');
【讨论】:
TypeError:Task.find 不是 Node Js 中的函数
】TypeError:Task.find不是NodeJs中的函数【英文标题】:TypeError:Task.findisnotafunctioninNodeJs【发布时间】:2021-01-0803:31:18【问题描述】:我的模型有问题。我已连接到MongoDB,并且我的服务器运行良好。但是我无法通过TaskController路由。... 查看详情
Node.js | TypeError: [...] 不是函数
】Node.js|TypeError:[...]不是函数【英文标题】:Node.js|TypeError:[...]isnotafunction【发布时间】:2018-03-1902:20:10【问题描述】:在我的主文件server.js中,我有以下功能:server.jsconstmongoose=require(\'mongoose\');constSmallRounds=require(\'./models/smallroun... 查看详情
node.js:typeerror:req.end不是一个函数(代码片段)
我得到一个req.end不是一个函数,当它在代码中的其他地方起作用时。从谷歌搜索中,我已经看到也许全局req变量丢失了吗?但是我没有使用任何其他名为req的变量...这仅在名字和姓氏检查循环内发生.....app.post('/createaccount',functio... 查看详情
类型错误:不是 NODE JS DIScord Bot 中的函数 [关闭]
】类型错误:不是NODEJSDIScordBot中的函数[关闭]【英文标题】:Typeerror:IsnotafunctioninNODEJSDIscordBot[closed]【发布时间】:2021-04-1217:01:29【问题描述】:constDBM=;constDiscordJS=require("discord.js");constBot=DBM.Bot=;Bot.on("ready",()=>console.log( 查看详情
为啥不和谐的客户端有错误输出:TypeError:node.js中的[CLIENT_MISSING_INTENTS]?
】为啥不和谐的客户端有错误输出:TypeError:node.js中的[CLIENT_MISSING_INTENTS]?【英文标题】:Whydoestheclientindiscordhaveanerrorthatoutputs:TypeError:[CLIENT_MISSING_INTENTS]innode.js?为什么不和谐的客户端有错误输出:TypeError:node.js中的[CLIENT_MISSIN... 查看详情
TypeError:无法读取未定义的属性“获取”(Node.js)
】TypeError:无法读取未定义的属性“获取”(Node.js)【英文标题】:TypeError:Cannotreadproperty\'get\'ofundefined(Node.js)【发布时间】:2017-02-0622:49:00【问题描述】:我是Node.js的新手,我需要帮助。我正在尝试使用express(^4.14.1)、路径(^0.1... 查看详情
TypeError:Firebase 不是函数
】TypeError:Firebase不是函数【英文标题】:TypeError:Firebaseisnotafunction【发布时间】:2016-09-1901:31:24【问题描述】:我正在尝试遵循firebase节点教程:https://www.firebase.com/docs/web/quickstart.html我的node.js应用程序因“”错误而崩溃。我的i... 查看详情
TypeError:Object.entries 不是函数
】TypeError:Object.entries不是函数【英文标题】:TypeError:Object.entriesisnotafunction【发布时间】:2017-10-0702:37:29【问题描述】:为什么我在尝试运行Node.js/Express服务器时不断收到此错误消息?这是新版ES7的一部分吗?我需要什么才能... 查看详情
TypeError:车把不是函数错误
】TypeError:车把不是函数错误【英文标题】:TypeError:handlebarsisnotafunctionerror【发布时间】:2022-01-1800:04:45【问题描述】:我是node.js的新手,我使用模板引擎express-handlebar,但我遇到的问题是:**TypeError:handlebarsisnotafunctionatObject.**... 查看详情
res.sendFile 不是 Node.js 的函数
...我遇到的错误Applicationhasthrownanuncaughtexceptionandisterminated:TypeError:res.sendFileisnotafunction 查看详情
TypeError: x(...).subscribe 不是函数
】TypeError:x(...).subscribe不是函数【英文标题】:TypeError:x(...).subscribeisnotafunction【发布时间】:2019-01-2622:04:37【问题描述】:我正在尝试对在ngOnInit中调用服务方法的组件进行单元测试。当我尝试运行测试时,出现错误:TypeError:thi... 查看详情
Node js .replace() 不是函数
...7:17【问题描述】:当我在节点js中运行此代码时,我得到TypeError:filter_list.replaceisnotafunction,我也尝试过使用toString但这似乎没有帮助,有人可以帮忙吗?varfilter_list=["abcd","efgh"];filte 查看详情
TypeError:即使定义了方法,“x”也不是函数
】TypeError:即使定义了方法,“x”也不是函数【英文标题】:TypeError:\'x\'isnotafunctionevenwhenthemethodisdefined【发布时间】:2021-10-2316:59:25【问题描述】:我正在对一个文件运行一些测试,我收到这个奇怪的错误为TypeError:this.activatedS... 查看详情
nodeenv 中的 x.x.x-rc Node.js 版本是啥?
】nodeenv中的x.x.x-rcNode.js版本是啥?【英文标题】:Whatarethex.x.x-rcNode.jsversionsinnodenv?nodeenv中的x.x.x-rcNode.js版本是什么?【发布时间】:2017-12-1216:16:40【问题描述】:安装nodenv和node-build后,我查看了可用的Node.js版本nodenvinstall--list... 查看详情
node.js / react 使用 graphql createReadStream 上传文件不是函数
...程序(react)发送到服务器(node.js)。我收到错误消息:TypeError:createR 查看详情
node.js/discord.js:TypeError:无法读取 null 的属性“setPresence”
】node.js/discord.js:TypeError:无法读取null的属性“setPresence”【英文标题】:node.js/discord.js:TypeError:Cannotreadproperty\'setPresence\'ofnull【发布时间】:2020-02-1622:19:19【问题描述】:尝试一个简单的测试,但我从这一行得到一个错误:clie... 查看详情
node.js“TypeError:无法读取未定义的属性'auth_token'”
】node.js“TypeError:无法读取未定义的属性\\\'auth_token\\\'”【英文标题】:node.js"TypeError:Cannotreadproperty\'auth_token\'ofundefined"node.js“TypeError:无法读取未定义的属性\'auth_token\'”【发布时间】:2021-11-2415:59:52【问题描述】:... 查看详情
node.js 中的嵌套承诺是不是正常?
】node.js中的嵌套承诺是不是正常?【英文标题】:Arenestedpromisesnormalinnode.js?node.js中的嵌套承诺是否正常?【发布时间】:2016-06-1818:03:03【问题描述】:我在学习node.js的过程中一直在努力解决的问题是如何使用node.js进行同步编... 查看详情