2017-02-02 18 views
1

我試圖做一個環回的模型JS文件的查詢。這是非常簡單的,它如下:相同的「發現」在環回和蒙戈查詢在不同的服務器不同的行爲

//isAdmin function: gets a boolean if a given userProfileId is an admin or not. 
    Userprofile.isAdmin = function(userProfileId, cb) { 
     let role = app.models.Role; 
     let rolemapping = app.models.RoleMapping; 

     let filter = { 
      "where": { 
       "name": "admin" 
      }, 
      "fields": ["id"] 
     }; 

     role.findOne(filter, function(err, instance) { 
      let filter2 = { 
       "where": { 
        "roleId": instance.id 
        , 
        "principalId": userProfileId, 
       }, 
       "fields": ["id"] 
      }; 

      rolemapping.find(filter2, function(err, instance) { 
       if (instance != "") { 
        cb(null, true); 
       } else { 
        cb(null, false); 
       } 
      }); 
     }); 
    }; 

這在我們的開發服務器中完美地工作。的filter2console.log,在服務器返回:

{ where: 
    { roleId: 5890ef8bbef9b73e568c6933, 
     principalId: '5890ef8bbef9b73e568c6932' }, 
     fields: [ 'id' ] 
    } 
} 

instance.idconsole.log樣子:

ObjectID { _bsontype: 'ObjectID', id: 'Xï¾ù·>Vi3 } 

的問題是,在我們的生產服務器。我已經做了部署過程和結果略有不同,但它並沒有在所有的工作:(

filter2console.log回報:

{ where: 
    { roleId: 58921dff16d9a37009e21104, 
      principalId: '5890ef8bbef9b73e568c6932' }, 
      fields: [ 'id' ] } 
} 

而且,console.log(instance.id)回報:

ObjectID { _bsontype: 'ObjectID', id: Buffer [ 88, 146, 29, 255, 22, 217, 163, 112, 9, 226, 17, 4 ] } 

這個查詢在我們的生產服務器不會返回任何文件,即使有在DB滿足查詢的文檔。

我比較了所有的npm軟件包,都是完全相同的版本。以及服務器(我們正在使用Debian 8)和mongo(v3.2.10)。

任何是否有一個想法,以解決此問題?

在此先感謝!

佩德羅。

回答

1

與相關模型的ID查詢時,我都遇到過類似的問題。通過選擇喜歡的,而不是像等價解決以下問題:

{where: 
    {and:[{roleId: {like:'.*'+roleIdVar+'*.', options:'i'}}, 
     {principalId: {like:'.*'+principalIdVar+'*.', options:'i'}}] 
    } 
}, 
{fields: [ 'id' ] } 

也請注意,現場的投影應該是一個獨立的對象從哪裏過濾器。

0

有你在 「用戶配置」 以.json文件檢查USERPROFILE ID類型?這似乎是一個ids類型的問題。你可以解決它的性質與添加字符串類型的ID,在你model.json文件中,這樣的事情:

"properties": { 
"userProfileId": { 
    "type": "string" 
} 
+0

是的,就是這樣!非常感謝! – pleivac

相關問題