2014-06-16 154 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领域。 如果有人可以改善这个答案,请不要怀疑在这里发布它。