Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.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
Node.js 要求在angularjs中给出错误_Node.js_Angularjs_Mongoose - Fatal编程技术网

Node.js 要求在angularjs中给出错误

Node.js 要求在angularjs中给出错误,node.js,angularjs,mongoose,Node.js,Angularjs,Mongoose,我使用的是带有nodejs的angular。 我创建了一个.js文件,在其中定义数据库连接。 文件看起来像: var mongoose = require('mongoose'); var db = mongoose.connect('mongodb://localhost/userregistration'); db.connection.on('open', function ($scope) { console.log('db open'); var newuserSche

我使用的是带有nodejs的angular。 我创建了一个.js文件,在其中定义数据库连接。 文件看起来像:

var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/userregistration');
db.connection.on('open', function ($scope) {
    console.log('db open');
    var newuserSchema = new mongoose.Schema({
        firstname: String,
        lastname: String,
        username: String,
        email: String,
        password: String
    });
    console.log('outside');
    var newuser = db.model(newuser, newuserSchema);

});
现在,我正在尝试使用require as在另一个.js文件中访问此文件:

require('../models/user_model.js');

registration.click = function(req, res){
console.log('inside');
    var user = new User({ username: $scope.NewUname});
    user.set('firstname', $scope.fName);
    user.set('lastname', $scope.lName);
    user.set('password', $scope.pwd);
    user.set('email', $scope.mail);

    console.dir(user);

    user.save(function(err){
       if(!err)
       {
           console.log('user added successfull');
           alert('user added successfull');
       }
    });

};
但是,当我运行代码时,它会给出如下错误:

Uncaught ReferenceError: require is not defined
  (anonymous function)  useradd.js:5

Require不是Javascript的一部分,而是node.js的一部分。所以我猜你调用node.js太晚了

您可以在问题中查看一些详细信息