2015-11-25 29 views
2

我有一個模型:SailsJS - 蒙戈找到地方或條件協會(一對一)

module.exports = { 

    schema: true, 

    attributes: { 

    friend1: { 
     model: "User", //Logged in User 
     required: true 
    }, 
    friend2: { 
     model: "User", //Profile Viewing User to whom I can send friend request 
     required: true 
    }, 
    status: { 
     type: "int", 
     defaultsTo: "0" 
    } 
    } 
}; 

現在我想檢查是否有用戶是我的朋友或不同意下面寫過濾器,似乎它不管用!

Friend.find().where({ 
    or: [ 
     { 
      friend1: user_id, 
      friend2: fried_id 
     }, 
     { 
      friend1: fried_id, 
      friend2: user_id 
     } 
    ] 
}).exec(function findCB(err, Friend) { 
    if(err) throw err; 
    next(Friend); 
}); 

任何人都可以找到我在這裏做錯了嗎?

回答

1

根據您使用的版本,How to use Search Modifier 「OR」 in Sails.js using Waterline Adapter for Mongo有某種相關的已關閉問題。作爲一種變通方法,我只能建議你嘗試使用native()其訪問表示指定型號原始蒙戈集合實例,讓您在執行原始蒙戈查詢:當

var criteria = { 
    "$or": [ 
     { "friend1": user_id, "friend2": friend_id }, 
     { "friend1": friend_id, "friend2": user_id } 
    ] 
}; 

// Grab an instance of the mongo-driver 
Friend.native(function(err, collection) {   
    if (err) return res.serverError(err); 

    // Execute any query that works with the mongo js driver 
    collection.find(criteria).toArray(function (err, friendObj) { 
     if(err) throw err; 
     console.log(friendObj); 
    }); 
}); 
+0

NP似乎它有一個問題呢,我使用上面的例子,我得到了一個錯誤: mongodb \ lib \ utils.js:98 process.nextTick(function(){throw err;}); ^ 類型錯誤:對象的翻譯:有沒有方法「EXEC」 –

+0

我甚至也低於指定者回調試過似乎這其中也沒有工作,我返回[]: Friend.native(函數(ERR,集合){ 如果(ERR)返回res.serverError(ERR); collection.find(條件).toArray(函數(ERR,friendObj){ 如果(ERR)擲ERR; 的console.log(friendObj); 下( );; }); –

+0

您使用的是什麼版本的Sails?當它工作時,你是如何運行腳本的? – chridam