Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.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
Javascript 如何:将node.js mongodb结果传递给angularjs?_Javascript_Angularjs_Node.js_Mongodb_Express - Fatal编程技术网

Javascript 如何:将node.js mongodb结果传递给angularjs?

Javascript 如何:将node.js mongodb结果传递给angularjs?,javascript,angularjs,node.js,mongodb,express,Javascript,Angularjs,Node.js,Mongodb,Express,所以我有一个MongoDB,我使用Node.js查询它 数据没有用这个功能发送出去,我不知道出了什么问题 var findIco = function(db, callback) { var cursor =db.collection('footIco').find(); cursor.each(function(err, doc) { // console.log(err); if (doc != null) { console.log(doc

所以我有一个MongoDB,我使用Node.js查询它

数据没有用这个功能发送出去,我不知道出了什么问题

var findIco = function(db, callback) {
   var cursor =db.collection('footIco').find();
   cursor.each(function(err, doc) {
     // console.log(err);
      if (doc != null) {
        console.log(doc); <------ DISPLAY THE DATA IN THE CONSOLE
      } else {
         callback();
      }
   });
}; 
app.get('/icons', function(req, res){
  //calling the function
  MongoClient.connect(url, function(err, db) {
    if(err) {
      console.log(err);
      return res.status(500).send(err);
   }

    findIco(db, function(icons) {
         res.header("Access-Control-Allow-Origin", "*");
   res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
  res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
  console.log(icons);<--------------- IS UNDEFINED
        res.json(icons);
        db.close();
        return;
    });
  });
});
app.listen(8080);
var findIco=函数(db,回调){
var cursor=db.collection('footIco').find();
游标。每个(函数(错误、文档){
//控制台日志(err);
如果(doc!=null){

console.log(doc);在您的angular代码中,您将有一个类似于
getDocument.controller.js的文件,其中包含一个对您的服务进行http调用的函数。类似于:

var getDocs = function(){
    var apis = http://localhost:9000/api/getDocs;
    httpRequest.get(apis).
    then(function(docs){
        // Your code to handle the response
    });
};
现在,在服务器端代码中,您可以将响应作为

CollectionName.find({}, function (err, docs) {
    if (err) return next(err);
    if (!docs) return res.send(401);
    res.json(docs);
});

您需要使用某种形式的Web服务器来创建一个API,当某个资源收到请求时,该API将返回此数据。您可以使用Angular应用程序中的
$http
服务向您的API发出请求。对于节点Web框架,最流行的选择是,这样可以包装节点核心并提供健壮的性能

其他流行的Node.js Web框架

这些只是几个Node.js Web框架,我还排除了任何基于MVC的框架,例如和,因为Angular已经提供了这一部分

要在Express中快速启动和运行,您可以使用构建一个基本的Express API,然后在Node.js服务器中为您的函数添加一个路由

findIco

var findIco = function(db, callback) { 
  db.collection('footIco').find().toArray(function(err, docs) {
    if (err) return callback(err, null);

    return callback(null, docs);
  }); 
} 
Node.js API

app.get('/icons', function getIcons(req, res){
  //calling the function
  MongoClient.connect(url, function(err, db) {
    if(err) {
      console.log(err);
      return res.status(500).json(err);
   }

    findIco(db, function(err, icons) {
      if (err) 
        res.status(500).json(err);
      else {
        if (!icons)
          res.status(204).send();
        else
          res.json(icons);
      }
      db.close();
      return;
    });
  });
});
角度
$http
调用
footIconCtrl

app.controller('footIconCtrl', ['$scope', '$http', function($scope, $http){
  $scope.icons = [];

  $http({
    method: 'GET',
    url: 'http://<serverAddress>:<serverPort>/icons'
  })
    .then(function(icons) {
      $scope.icons = icons.data;
    })
    .catch(function(errRes) {
      // Handle errRess
    });
});
app.controller('footIconCtrl',['$scope','$http',函数($scope,$http){
$scope.icons=[];
$http({
方法:“GET”,
url:'http://:/icons'
})
.然后(功能(图标){
$scope.icons=icons.data;
})
.catch(函数(errRes){
//手柄错误
});
});

我如何定义$http?您不定义它,它在核心Angular模块中提供。您从控制器访问它的方式是将它注入到控制器的依赖项中,如下所示:
app.controller('footIconCtrl',['$scope','$http',function($scope,$http){});
im收到此错误XMLHttpRequest无法加载。请求的资源上不存在“访问控制允许源站”标题。因此,不允许访问源站“”。您的Angular应用程序需要由express应用程序提供服务,或者它们需要位于同一域,即,它们都必须来自
localhost
。请参阅此问题n和answer代码示例应该可以帮助您解决此问题。