gpt4 book ai didi

javascript - require() 抛出 “AssertionError: missing path” - Node js

转载 作者:搜寻专家 更新时间:2023-11-01 00:12:38 24 4
gpt4 key购买 nike

我正在尝试一个简单的 nodejs 应用程序。一切正常,直到我将数据库模型分离到用户文件中并在 routes.js 中要求它。然后当我尝试运行服务器。

routes.js (path='demo/app/routes.js')

var User=require('./models/user');

module.exports=function(app){
app.get('/',function(req,res){
res.send("hello world");
});

app.get('/:username/:password',function(req,res){
var newUser =new User();
newUser.local.username=req.params.username;
newUser.local.password=req.params.password;
console.log(newUser.local.username);
newUser.save(function(err){
if(err){
throw err;
}
});
res.send('success');
});
}

user.js (path='demo/app/models/user.js')

var mongoose=require(mongoose);

var userScheme=mongoose.Schema({
local:{
username:String,
password:String
}
});

module.exports=mongoose.model('User',userScheme);

server.js运行文件(demo/server.js)

var express=require('express');
var app=express();
var port =process.env.PORT||3000;

var morgan=require('morgan');
var cookieParser=require('cookie-parser');
var session=require('express-session');
var mongoose=require('mongoose');

var configDB=require('./config/database.js');
mongoose.connect(configDB.url);

app.use(morgan('dev'));
app.use(cookieParser());
app.use(session({
secret:'pokemonGo',
saveUninitialized:true,
resave:true
}));

require('./app/routes.js')(app);

app.listen(port);
console.log('server running ');

错误说

assert.js:90
throw new assert.AssertionError({
^
AssertionError: missing path
at Module.require (module.js:351:3)
at require (internal/module.js:12:17)
at Object.<anonymous> (/Users/menaka/WebstormProjects/cardCreaterServer/app/models/user.js:4:14)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/Users/menaka/WebstormProjects/cardCreaterServer/app/routes.js:4:10)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)

最佳答案

语法错误

改变

var mongoose=require(mongoose);

到-->

var mongoose=require('mongoose');

特别感谢@YuryTarabanko

关于javascript - require() 抛出 “AssertionError: missing path” - Node js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38541545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com