代码之家  ›  专栏  ›  技术社区  ›  techStud

光标。forEach不是功能节点.js

  •  1
  • techStud  · 技术社区  · 8 年前

    forEach公司

    这是我的代码:

        var fetch = function(callback) {
        // mongoose.connect('mongodb://localhost/registration');
        mongoose.connect('mongodb://localhost/questiondb');
        var cursor = user.find();
       cursor.forEach(function (doc,err) {
             if(!err)
                 resultArray.push(doc);
             else 
             {
                  callback(false);
             }
       },
         function () {
              callback(resultArray);
         });
    };
    

    错误为: TypeError:光标。forEach不是函数

    3 回复  |  直到 8 年前
        1
  •  1
  •   JohnnyHK    8 年前

    猫鼬 Model.find Query find ,你会得到你想要创造的行为 fetch

    var fetch = function(callback) {
        // Don't connect here, connect once during startup
        // mongoose.connect('mongodb://localhost/questiondb');
    
        user.find((err, resultArray) => {
            if (!err) {
                callback(resultArray);
            }
            else {
                callback(false);
            }
        });
    };
    
        2
  •  0
  •   CESAR NICOLINI RIVERO    8 年前

    您需要在数组中转换

    User.find(function (err,userx){
        if (err){
            throw err;
        }
        else{
            //add this code sentence
            var user_arr =Object.keys(userx).map(
                function(key){
                    return userx[key];
                }
            );
    
            //now, use forEach sentence
            user_arr.forEach(function(us){
                console.log(us.name);
            })
    
        }
    })  
    
        3
  •  -2
  •   Ariana    8 年前

    因为光标是 .forEach函数必须用于数组。

    推荐文章