Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.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
Angularjs angular.js-TypeError:无法读取属性';然后';未定义的_Angularjs_Node.js_Mongodb_Service_Mongoose - Fatal编程技术网

Angularjs angular.js-TypeError:无法读取属性';然后';未定义的

Angularjs angular.js-TypeError:无法读取属性';然后';未定义的,angularjs,node.js,mongodb,service,mongoose,Angularjs,Node.js,Mongodb,Service,Mongoose,我正在尝试使用我的服务插入每个捕获的详细信息页面 虽然当我尝试在我的服务中加载带有id的页面时(例如:/capture/5740c1eae324ae1f19b7fc30),我得到了未定义的代码: app.factory('captureApi', ['$http', function($http){ var urlBase = 'URL'; // ==> Gives back an array as such: //[ //{ // "_id": "5740c1e3e324ae1f19

我正在尝试使用我的服务插入每个捕获的详细信息页面

虽然当我尝试在我的服务中加载带有id的页面时(例如:
/capture/5740c1eae324ae1f19b7fc30
),我得到了未定义的代码:

app.factory('captureApi', ['$http', function($http){

var urlBase = 'URL';
// ==> Gives back an array as such:
//[
//{
//  "_id": "5740c1e3e324ae1f19b7fc2f",
//  "created_at": "2016-05-21T20:15:38.554Z",
//  "userId": "118000609287736585411",
//  "place": "place",
// "birdname": "birdname",
//  "__v": 0
//},
//{
//  "_id": "5740c1eae324ae1f19b7fc30",
//  "created_at": "2016-05-21T20:15:38.554Z",
//  "userId": "118000609287736585411",
//  "place": "place",
//  "birdname": "birdname",
//  "__v": 0
//},
//{ 
//  ...
//}
//]

return {
    getAllCaptures : function () {
        return $http.get(urlBase);
    },

    insertCapture : function(data) {
        return $http.post(urlBase, data);
    },

    findCapture : function(id) {
        //both give undefined
        console.log(_.find(urlBase, function(capture){return capture._id == id}));
        console.log(_.find(urlBase, function(capture){return capture.id == id}));
        return _.find(urlBase, function(capture){return capture.id == id});
    }
    }
}]);
在服务器端,我使用mongoose/mongodb:

-路线:

var Capture = require('../models/capture');
module.exports = function(router) {
    router.post('/captures', function(req, res){
        var capture = new Capture();
        capture.birdname = req.body.birdname;
        capture.place =  req.body.place;
        capture.userId = req.body.userId;
        capture.author = req.body.author;
        capture.picture = req.body.picture;
        capture.created_at = new Date();

        capture.save(function(err, data){
            if(err)
                throw err;
            console.log(req.body);
            res.json(data);
        });
    });

    router.get('/captures', function(req, res){
        Capture.find({}, function(err, data){
            res.json(data);
        });
    });

     router.delete('/captures', function(req, res){
          Capture.remove({}, function(err){
              res.json({result: err ? 'error' : 'ok'});
          });
      });

    router.get('/captures/:id', function(req, res){
         Capture.findOne({_id: req.params.id}, function(err, data){
             res.json(data);
         });
     });

     router.delete('/captures/:id', function(req, res){
         Capture.remove({_id: req.params.id}, function(err){
             res.json({result: err ? 'error' : 'ok'});
         });
     });

    // router.post('/captures/:id', function(req, res){
    //     Capture.findOne({_id: req.params.id}, function(err, data){
    //         var capture = data;
    //         capture.birdname = req.body.birdname;
    //         capture.place.city = req.body.place.city;
    //         capture.place.country = req.body.place.country;

    //         capture.save(function(err, data){
    //             if(err)
    //                 throw err;
    //             res.json(data);
    //         });
    //     })
    // })
}
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var captureSchema = mongoose.Schema({
    birdname: {type: String, required: true},
    place: {type: String, required: true},
    userId: {type: String, required: true},
    author: {type: String, required: true},
    picture: Schema.Types.Mixed,
    created_at: Date
});

module.exports = mongoose.model('Capture', captureSchema)
-型号:

var Capture = require('../models/capture');
module.exports = function(router) {
    router.post('/captures', function(req, res){
        var capture = new Capture();
        capture.birdname = req.body.birdname;
        capture.place =  req.body.place;
        capture.userId = req.body.userId;
        capture.author = req.body.author;
        capture.picture = req.body.picture;
        capture.created_at = new Date();

        capture.save(function(err, data){
            if(err)
                throw err;
            console.log(req.body);
            res.json(data);
        });
    });

    router.get('/captures', function(req, res){
        Capture.find({}, function(err, data){
            res.json(data);
        });
    });

     router.delete('/captures', function(req, res){
          Capture.remove({}, function(err){
              res.json({result: err ? 'error' : 'ok'});
          });
      });

    router.get('/captures/:id', function(req, res){
         Capture.findOne({_id: req.params.id}, function(err, data){
             res.json(data);
         });
     });

     router.delete('/captures/:id', function(req, res){
         Capture.remove({_id: req.params.id}, function(err){
             res.json({result: err ? 'error' : 'ok'});
         });
     });

    // router.post('/captures/:id', function(req, res){
    //     Capture.findOne({_id: req.params.id}, function(err, data){
    //         var capture = data;
    //         capture.birdname = req.body.birdname;
    //         capture.place.city = req.body.place.city;
    //         capture.place.country = req.body.place.country;

    //         capture.save(function(err, data){
    //             if(err)
    //                 throw err;
    //             res.json(data);
    //         });
    //     })
    // })
}
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var captureSchema = mongoose.Schema({
    birdname: {type: String, required: true},
    place: {type: String, required: true},
    userId: {type: String, required: true},
    author: {type: String, required: true},
    picture: Schema.Types.Mixed,
    created_at: Date
});

module.exports = mongoose.model('Capture', captureSchema)
这是我的server.js(额外信息):


在客户端,我在页面上有以下触发器:

 $stateProvider

    .state('home', {
      url: '/',
      templateUrl: 'partials/home.html',
      controller: 'homeCtrl',
        data: {
            requiresLogin: false
        },
        resolve: {
          $title: function() { return 'Home'; }
        }
    })

    .state('dash', {
      url: '/dashboard',
      templateUrl: 'partials/dashboard.html',
      controller: 'dashCtrl',
        data: {
            requiresLogin: true
        },
        resolve: {
          $title: function() { return 'Dashboard'; }
        }
    })

    .state('capture', {
      url: '/capture',
      templateUrl: 'partials/capture.html',
      controller: 'captureCtrl',
        data: {
            requiresLogin: true
        },
        resolve: {
          $title: function() { return 'Capture'; }
        }
    })

    .state('viewCapture', {
      url: '/capture/:id',
      templateUrl: 'partials/viewCapture.html',
      controller: 'viewCaptureCtrl',
      data: {
            requiresLogin: true
        },
      resolve: {
        $title: function() { return 'Capture Detail'; }        
      }
    })
viewCaptureCtrl.js:

app.controller('viewCaptureCtrl', ['$scope', 'captureApi', '$stateParams', '$http', function($scope, captureApi, $stateParams, $http) {

     var id = $stateParams.id;

      $scope.viewCapture = function() {
        captureApi.findCapture(id)
            .then(function(data) {
              $scope.capture = data;
            });
      };

      $scope.viewCapture();

}]);
有人知道为什么我的find函数给出了一个未定义的值吗?
非常感谢您的帮助!谢谢

您需要引用
下划线并将其注入到您的服务中。文件:

编辑:但我不熟悉
\uu.find()
;它是否还承诺。如果没有,您需要使用
$q
创建承诺并返回它,以便使用
then()


您确定
findCapture(id)
正在返回承诺吗?它未定义。我猜我的服务出了问题,但不知道是什么原因。输出仍然提供了未定义的
,但感谢已经存在的可能需要在将来修复的问题。@CedricBongaerts我不认为
。.find()
会返回一个承诺。您需要使用
$q
。请参阅编辑。您的代码给出了
错误:[$injector:unpr]未知提供程序:,$qProvider我当时不知道。如果您需要更多帮助,这里有$q的文档。我犯了一个错误,它是正确的,我猜它写错了。不管怎样,它现在给出了一个承诺,但其值尚未定义<代码>承诺{$$state:Object}$$state:Object status:1值:未定义的_proto__:Object _proto__:Object
//inject into capture api factory
app.factory('captureApi', ['$http', '_', '$q',
    function($http, _, $q) {
        return {
            findCapture: function(id) {

                var deferred = $q.defer();

                try {
                    var results = _.find(); //do stuff with _
                    deferred.resolve(results);
                } catch (err) {
                    deferred.reject(err);
                }

                //you need to return a promise in order to use then()
                return deferred.promise;

            }
        }
    }
]);