【问题标题】:try to fetch data from mongdb but getting error尝试从 mongodb 获取数据但出现错误
【发布时间】:2016-04-05 22:43:03
【问题描述】:

我正在尝试获取与相关登录用户关联的数据,但我收到错误,例如 Error: [filter:notarray] Expected array but received。

控制器文件:

function ManageProductController($http, $scope, $mdDialog, $document, $location, $localStorage)
{
     var vm = this;
     vm.uid = $localStorage._id;

     $http({
            url: 'http://localhost:7200/api/manage-product',
            method: 'POST',
            data: {userId:vm.uid}
        }).success(function(res) {
            //$scope.productlist = res;
            //console.log(res.result);
            vm.result=res.result;

            //vm.docs=res.docs;
        }, function(error) {
            console.log(error);
            alert('here');
        });
 }

节点文件:

router.post('/manage-product', function(req, res){
    //console.log('I received get request');

    //console.log(req.body.userId);
    var um = ObjectId(req.body.userId);
    //console.log(um);
    var findProducts = function(db, callback) {

       var cursor =db.collection('proInfo').findOne({userId:um},function(err, docs){
          if(err){

             callback(new Error("Some problem"));
           }else{
            callback(null,docs);
        } 
         });

    };


    MongoClient.connect(url, function(err, db) {
      assert.equal(null, err);
        findProducts(db, function(err,docs) {
          db.close();
          if(err) return res.json({result:null})
        else
       return res.json({result: docs  });
      });
    });
 });

我得到如下图的结果

我的数据库如下图所示

【问题讨论】:

    标签: angularjs node.js mongodb express


    【解决方案1】:

    您的前端代码需要一个数组。你给它一个对象{ response: your_resultant_array }。这就是您收到此错误的原因。要解决此问题,请在您的 node.js 文件中进行以下更改:

    MongoClient.connect(url, function(err, db) {
      assert.equal(null, err);
        findProducts(db, function(err,docs) {
          db.close();
          if(err) return res.json(err)
          else return res.json(docs);
        });
    });
    

    另外,您使用的是findOne,它只会返回一种产品。如果您想要多个产品,请使用find。像这样的:

    var findProducts = function(db, callback) {
    
       var cursor = db.collection('proInfo').find({userId:um}).toArray(function(err, docs){
          if(err) callback(new Error("Some problem"));
          else if(docs) callback(null,docs);
         });
    
    };
    

    希望这会有所帮助。

    【讨论】:

    • 是的,错误已清除,但该用户有 2 个产品,但我在控制台中只得到一个产品。
    • 那是因为在您的findProducts 中您正在调用findOnefindOne 总是返回一个文档。如果您想要多个文档,请使用find。让我更新答案。
    • 而不是 findone 如果我使用 find 它会显示以下错误 TypeError: Converting circular structure to JSON at Object.stringify (native)
    • 我没有在任何地方使用它
    【解决方案2】:

    好吧,错误说明了一切 - “预期的数组,但收到了” - 您的单个对象的 JSON。你得到它是因为你使用了findOne,这意味着你得到一个对象而不是对象数组。

    【讨论】:

    • 我应该怎么做才能得到对象数组?
    【解决方案3】:
    router.post('/manage-product', function(req, res){
        //console.log('I received get request');
    
        //console.log(req.body.userId);
        var um = ObjectId(req.body.userId);
        //console.log(um);
        var findProducts = function(db, callback) {
    
           var cursor =db.collection('proInfo').find({userId:um}).toArray(function(err, docs){
              if(err){
    
                 callback(new Error("Some problem"));
               }else{
                callback(null,docs);
            } 
             });
    
        };
    
        MongoClient.connect(url, function(err, db) {
          assert.equal(null, err);
            findProducts(db, function(err,docs) {
              db.close();
              if(err) return res.json({result:null})
            else
           return res.json({result: docs  });
          });
        });
    

    我以前就是这样,现在我得到了我想要的。

    【讨论】:

    • 忘记了.toArray 部分。节点驱动程序的find 方法,返回一个游标。 toArray 用尽它来获取一个数组。
    猜你喜欢
    • 1970-01-01
    • 2016-05-12
    • 2022-09-24
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 2022-01-02
    • 2019-09-03
    • 1970-01-01
    相关资源
    最近更新 更多