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');
  }
});

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

提前致谢。

4

1 回答 1

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。如果有人可以改进此答案,请不要怀疑将其发布在这里。

于 2014-06-17T02:40:44.123 回答