【问题标题】:SImple search function in YDN-DBYDN-DB中的简单搜索功能
【发布时间】:2014-08-06 19:40:55
【问题描述】:

我正在尝试制作一个简单的函数,允许我在特定表中搜索特定项目并使用 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');
  }
});

现在的问题是,这段代码根本不起作用。你能帮我或指出我哪里错了吗?

提前致谢。

【问题讨论】:

    标签: javascript ydn-db


    【解决方案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。 如果有人可以改进这个答案,请不要怀疑在这里发布。

    【讨论】:

      猜你喜欢
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-04
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 2013-06-03
      相关资源
      最近更新 更多