Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function and module.exports error issue NODE.js_Node.js_Function_Random - Fatal编程技术网

Function and module.exports error issue NODE.js

Function and module.exports error issue NODE.js,node.js,function,random,Node.js,Function,Random,:) 我有一个简单的答案,你们可以像往常一样回答。 我是函数之类的新手,我看了一些关于将函数导出到另一个node.js应用程序的教程 我正在尝试为外部模块生成一些随机数 这就是我的设置 (index.js文件) (run.js) 我的问题是,当我运行index.js文件时,我从控制台得到这个错误 TypeError: Object #<Object> has no method 'randNumb' at Object.<anonymous> (C:\User

:)

我有一个简单的答案,你们可以像往常一样回答。 我是函数之类的新手,我看了一些关于将函数导出到另一个node.js应用程序的教程

我正在尝试为外部模块生成一些随机数

这就是我的设置

(index.js文件)


(run.js)


我的问题是,当我运行index.js文件时,我从控制台得到这个错误

TypeError: Object #<Object> has no method 'randNumb'
    at Object.<anonymous> (C:\Users\Christopher Allen\Desktop\Node Dev Essential
     s - Random Number\index.js:8:16)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)
TypeError:对象#没有方法“randNumb”
反对。(C:\Users\Christopher Allen\Desktop\Node Dev Essential
s-随机数\index.js:8:16)
在模块处编译(Module.js:449:26)
在Object.Module._extensions..js(Module.js:467:10)
在Module.load(Module.js:356:32)
在Function.Module.\u加载(Module.js:312:12)
位于Module.runMain(Module.js:492:10)
在process.startup.processNextTick.process.\u tickCallback(node.js:244:9)
一开始我运行了run.js,这就是我得到的

    ReferenceError: randNumb is not defined
    at Object.<anonymous> (C:\Users\Christopher Allen\Desktop\Node Dev Essential
    s - Random Number\run.js:3:1)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)
ReferenceError:未定义randNumb
反对。(C:\Users\Christopher Allen\Desktop\Node Dev Essential
s-随机数\run.js:3:1)
在模块处编译(Module.js:449:26)
在Object.Module._extensions..js(Module.js:467:10)
在Module.load(Module.js:356:32)
在Function.Module.\u加载(Module.js:312:12)
位于Module.runMain(Module.js:492:10)
在process.startup.processNextTick.process.\u tickCallback(node.js:244:9)

您根本没有导出var

您的index.js应该是:

function randNumb(topnumber) {
   return Math.floor(Math.random()*topnumber)
}
module.exports.randnumber = randNumb(10); //replace 10 with any other number...
var index = require("./run.js");
console.log(index.randnumber);
run.js应该是:

function randNumb(topnumber) {
   return Math.floor(Math.random()*topnumber)
}
module.exports.randnumber = randNumb(10); //replace 10 with any other number...
var index = require("./run.js");
console.log(index.randnumber);

在randNum函数中,不要忘记:

return randnumber;
同样在index.js中,导出函数如下:

exports.randNumb = randNumb;
在run.js中这样调用它:

console.log(randNumber(10));

阅读本文,我将运行文件设置为var index=require(“./index.js”);console.log(index.randnumber);我将索引文件作为函数randNumb(topnumber){return Math.floor(Math.random()*topnumber)}module.exports.randnume=randNumb();我刚刚从控制台
module.exports.randnumber=randNumb(10)中得到一个NaN定义