Javascript Node JS 需要抛出 AssertionError:缺少路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32917131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node JS Require throw AssertionError: missing path
提问by JimZer
I have a simple application written in node.js:
我有一个用 node.js 编写的简单应用程序:
var mongo = require('./helpers/mongo_utils.js');
var express = require('express');
var user = require('./models/users.js');
mongo.connect(function (err)
{
if (err) throw err;
console.log('connected');
var app = express();
app.listen(3000, function ()
{
console.log('Server set up and start listening on port 3000');
})
})
All works except when I require the users.js file. If I don't require it I have no problem, but when I do it I get this error:
除非我需要 users.js 文件,否则一切正常。如果我不需要它,我没有问题,但是当我这样做时,我会收到此错误:
assert.js:89
throw new assert.AssertionError({
^
AssertionError: missing path
at Module.require (module.js:363:3)
at require (module.js:384:17)
at Object.<anonymous> (/home/jimzer--jimzer/www/NodeJsForum/models/users.js:1:79)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/home/jimzer--jimzer/www/NodeJsForum/app.js:3:12)
Here is the code of users.js if it can help:
如果有帮助,这里是 users.js 的代码:
var mongo = require('') = ('./../helpers/mongo_utils.js');
var mailValid = require('./../helpers/email_valid.js');
var db = mongo.getDb();
var User = function (pseudo, psw, mail, level, callback)
{
// Params checking
if (!(pseudo && psw && mail && (level != 'undefined')))
{
err = new Error("All fields aren't specified"); err.code = 0;
return callback(err);
}
// Mail validation
if (!mailValid(mail))
{
err = new Error("Mail adress isn't valid"); err.code = 1;
return callback(err);
}
db.users.findOne({mail: mail}, function (err, doc)
{
if (err) throw err;
if (doc)
{
err = new Error("Mail adress already used");
err.code = 1;
return callback(err);
}
});
// Pseudo
if (!(pseudo.length > 0 && pseudo.length < 20))
{
err = new Error("Pseudo length invalid");
err.code = 2;
return callback(err);
}
db.getDb.users.findOne({_id: pseudo}, function (err, doc)
{
if (err) throw err;
if (doc)
{
err = new Error("pseudo déja utilisé"); err.code = 2;
return callback(err);
}
});
// Psw validation
if (!(psw instanceof String))
{
err = new Error("Password invalid"); err.code = 3;
return callback(err);
}
// Level validation
if (!(lvl > 0 && lvl < 10))
{
err = new Error("Access level invalid"); err.code = 4;
return callback(err);
}
// If all test are OK, we construct and instance of User and pass it to the callback
else
{
this.pseudo = pseudo;
this.psw = psw;
this.mail = mail;
this.date = Date.now();
this.lvl = lvl;
return callback(null, this);
}
}
module.exports = User;
采纳答案by Daniel Conde Marin
Read the stacktrace:
阅读堆栈跟踪:
at Object.<anonymous> (/home/jimzer--jimzer/www/NodeJsForum/models/users.js:1:79)
This is wrong:
这是错误的:
var mongo = require('') = ('./../helpers/mongo_utils.js');
Not sure what you are trying to achieve, but should probably be this instead:
不确定您要实现的目标,但可能应该是这样的:
var mongo = ('./../helpers/mongo_utils.js');
回答by KARTHIKEYAN.A
You forget quotes around expressstring. Try to change this line:
您忘记了express字符串周围的引号。尝试更改此行:
var express = require(express);
for the following one:
对于以下一项:
var express = require('express');
and see if it fixed the problem.
看看它是否解决了问题。
回答by Shanimal
The error is caused by passing require
any untruthy value.
该错误是由传递require
任何不真实的值引起的。
var sinon = require(sinon);
what I really meant...
我真正的意思是...
var sinon = require('sinon');