简体   繁体   English

无法从其他文件调用函数:“ updateDB.inputFormToDB不是函数”

[英]Trouble calling a function from a different file: “updateDB.inputFormToDB is not a function”

Github source for reference Github来源供参考

Files where issue exists: updateDB.js , quickstart.js 存在问题的文件: updateDB.jsquickstart.js

Inside quickstart.js I have set a variable updateDB on line 2: quickstart.js中,我在第2行设置了一个变量updateDB

var updateDB = require('./updateDB.js');

which I believe refers to my updateDB.js file (which is currently living in the same folder). 我相信这是指我的updateDB.js文件(当前位于同一文件夹中)。

However later in the file when I try to call a function from updateDB.js on line 118: 但是稍后在文件中,当我尝试在第118行从updateDB.js调用函数时:

    updateDB.inputFormToDB(rows);

I get the error " updateDB.inputFormToDB is not a function ". 我收到错误“ updateDB.inputFormToDB is not a function ”。

Inside updateDB.js I have things set up as follows: updateDB.js内部,我将进行以下设置:

var updateDB= function() {
 some function    
 var inputFormToDB = function(parameter) {
     function code
 }
 some function
 some function
};
module.exports = updateDB;

Am I missing something to call my function from inside quickstart.js ??? 我是否想从quickstart.js内部调用函数? I feel like I'm making some small mistake somewhere. 我觉得我在某个地方犯了一些小错误。

The problem is in 问题出在

var updateDB = function() {...}

should be 应该

var updateDB = {...}

like a Object. 像一个对象。

eg 例如

var updateDB = {
  inputFormToDB: function() {...}
}

or 要么

var updateDB = function() {
  var x = ...

  function inputFormToDB() {...}

  return {
    inputFormToDB: inputFormToDB
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM