Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
991 views
in Technique[技术] by (71.8m points)

node.js - require is not defined in angular

I am using angularjs where i have created a js file(loginCtrl.js) and i have inclused a controller. I have defined my db connection and schema in another file(server.js) and i want to include that file in my js.

My loginCtrl.js looks like:

test.controller('loginCtrl', function($scope, loginService){

   $scope.login = function(user)
   {
       console.log('inside function 1');
       var user = require('./server.js')
      console.log('inside function');
       loginService.login(user);
   }

});

and my server.js files looks like:

var express = require('express');
var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/userregistration');

var userSchema = {
    username: String,
    firstname:String,
    lastname: String,
    email: String,
    password: String
}

var User = mongoose.model('User', userSchema, 'user');

When i run the code, it gives the error like:

ReferenceError: require is not defined

It is printing the console.log defined above.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

require() is a NodeJS function, your angular controller will be executed in the browser, which doesn't have that built-in function. If you want to replicate that behavior client-side, you should look at RequireJS


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...