w88975
V2EX  ›  Node.js

mongoose 查询不出来数据怎么办?

  •  2
     
  •   w88975 · Aug 17, 2015 · 4151 views
    This topic created in 3973 days ago, the information mentioned may be changed or developed.

    这是代码

    var mongoose = require ('mongoose');
    var db = mongoose.createConnection ('mongodb://xxxxxxxxxxxxxxxxx/test');
    
    db.on ('error', function (error ) {
        console.log (error );
    });
    
    var downloadSchema = new mongoose.Schema ({
        assets :  {type: String},
        body :  {type: String},
        tag_name :  {type: String},
        date :  {type: String},
    });
    
    var mongooseModel = db.model ('download', downloadSchema );
    
    mongooseModel.find (function (error,result ){
        if (error ) {
            console.log (result );
        } else {
            console.log (result ); // result 为空
        }
        db.close ();
    });
    

    查询出来的数据为 [],不知道原因出在哪里,求大大解决

    17 replies    2015-08-18 01:39:38 +08:00
    minvacai
        1
    minvacai  
       Aug 17, 2015
    不会 js ,不过你的 find 里那个 if/else 有必要么...
    w88975
        2
    w88975  
    OP
       Aug 17, 2015
    @minvacai .... 写错了而已,不要在意这些细节 err 为空
    minvacai
        3
    minvacai  
       Aug 17, 2015
    话说,你 result 是哪里来的, find 里没有具体的查询语句,也没见哪里调用 find 了啊
    w88975
        4
    w88975  
    OP
       Aug 17, 2015
    @minvacai 这是 mongoose 的查询,表在
    ``var mongooseModel = db.model ('download', downloadSchema );``

    db.model ('download'.... 这里的 download 是表
    Dongdong36
        5
    Dongdong36  
       Aug 17, 2015
    数据库中的集合名称叫什么
    minvacai
        6
    minvacai  
       Aug 17, 2015

    我是用 pymongo 的,还是不胡说八道给你添乱了
    Dongdong36
        7
    Dongdong36  
       Aug 17, 2015
    @w88975 集合名确认一下是否为“ downloads ”
    chaim
        8
    chaim  
       Aug 17, 2015
    mongoose.connect ('mongodb://xxxxxxxxxxxxxxxxx/test');

    var downloadSchema = new mongoose.Schema ({
    assets : {type: String},
    body : {type: String},
    tag_name : {type: String},
    date : {type: String},
    });

    var mongooseModel = mongoose.model ('download', downloadSchema );

    mongoose.connection.on ('connected', function () {
    mongooseModel.find (function (error,result ){
    if (error ) {
    console.log (result );
    } else {
    console.log (result ); // result 为空
    }
    });
    });

    我理解大致是这样子的
    w88975
        9
    w88975  
    OP
       Aug 17, 2015
    @Dongdong36 download 这个表存在
    gyteng
        10
    gyteng  
       Aug 17, 2015
    if (error ) {
    console.log (result );
    } else {
    console.log (result );
    }
    db.close ();
    楼主错在这个地方
    whimsySun
        11
    whimsySun  
       Aug 17, 2015   ❤️ 1
    mongoose 的 collection ,不指定名字的话,默认为 model name + s ,你这里应该是 downloads
    w88975
        12
    w88975  
    OP
       Aug 17, 2015
    @whimsySun collection 是提前创建好的,不是用 mogoose 创建的
    w88975
        13
    w88975  
    OP
       Aug 17, 2015
    @gyteng 这里把 err 写成 result 了 错并不在这里
    whimsySun
        14
    whimsySun  
       Aug 17, 2015
    `new Scheme ({...define}, {collection: 'download'})` 这样指定`collection`名
    jinwyp
        15
    jinwyp  
       Aug 18, 2015
    mongooseModel.find (function (error,result ){


    应该为
    mongooseModel.find ({}, function (error,result ){

    第一个参数是查询条件, 你直接漏了
    faceair
        16
    faceair  
       Aug 18, 2015
    楼上说的是对的 conditions 参数不能省略
    w88975
        17
    w88975  
    OP
       Aug 18, 2015
    @faceair 谢谢仁兄的方法,在 Scheme 你指定了 collection 名后,查询正常了,正解。√
    @jinwyp @faceair 经过测试,发现不用{},直接省略这个参数也可以全部查询,不过为了代码可读性,还是按你的写法来了。

    谢谢大家
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2975 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 74ms · UTC 07:34 · PVG 15:34 · LAX 00:34 · JFK 03:34
    ♥ Do have faith in what you're doing.