2014-06-16 156 views
0

我試圖做一個簡單的功能,讓我來搜索特定的表內的具體項目,並返回使用YDN-DB所期望的結果,到目前爲止,我有這樣的:簡單的搜索功能

var simpleSearch = function(table,field,string,limit,callback){ 
    var look = db.from(table).where(field, '=', string).list(limit); 
    look.done(function(result) { 
     callback(true,result); 
    }); 
    look.fail(function() { 
     callback(false,''); 
    }); 
} 
//usage 
simpleSearch('mytable','fieldname','nice field',1,function(found,result){ 
    if(found){ 
    console.log('item '+result.fieldname+' found'); //on success should output 'item nice field found' 
    }else{ 
    console.log('nothing found'); 
    } 
}); 

現在的事情是,這段代碼根本不起作用。你能幫我或指出我在哪裏錯了嗎?

在此先感謝。

回答

1

好吧,我想我找到了解決辦法:

var simpleSearch = function(table,field,operator,string,limit,callback){  
    var look = db.from(table).where(field, operator, string).list(limit); 
    look.done(function(result){ 
     if(result.length > 0){ 
      console.log('search found'); 
      callback(true,result); 
     }else{ 
      console.log('search not found'); 
      callback(false,''); 
     } 
    }); 
} 

//usage 

simpleSearch('users','id','=',userId,1,function(found,result){ 
      if(found){ 
       console.log(result.name); 
      }else{ 
       //user wasn't found, do something about it 
      } 
     }); 

確保在模式中添加您要搜索的keypath領域。 如果有人可以改善這個答案,請不要懷疑在這裏發佈它。