简体   繁体   English

自动完成JavaScript

[英]Autocomplete javascript

I have some problem in crating an autocomplete search box. 我在建立自动完成搜索框时遇到了一些问题。 I have a mongodb collection in which there are photos object with name, description, path and so on. 我有一个mongodb集合,其中包含带有名称,描述,路径等的photos对象。 Now, I created a route /searchbox, where the box is displayed in the browser. 现在,我创建了一个路由/ searchbox,该框显示在浏览器中。 Every time that the user press a key, a get request to the route /autocomplete/:query is made. 每次用户按下一个键,都会对路由/ autocomplete /:query发出获取请求。 The autocomplete route will search in the collection for all the objects where the name, the description or the keywords fields starts with the give query. 自动完成路由将在集合中搜索名称,说明或关键字字段以Give查询开头的所有对象。 Then it return a json object containing all the strings that will be put into a datalist in the view. 然后,它返回一个json对象,其中包含将放入视图中数据列表的所有字符串。 The problem is that I can't create that json array, I tried to create a json object with a field containing an array, and at every iteration on the found array returned by the find function, I get the field name and push it into the array, but nothing is added... here my code: 问题是我无法创建该json数组,我尝试创建一个包含包含数组的字段的json对象,并且在find函数返回的find数组的每次迭代中,我都获取了字段名并将其推入数组,但未添加任何内容...这是我的代码:

exports.autoComplete = function(req, res) {
  var PhotoAlbum = db.model('PhotoAlbum', schemas.PhotoAlbumSchema);
  var regexp = "^"+req.params.query;
  var suggestions = {suggestion: []};

  var strings = "";
  var arrayStrings = [];


  PhotoAlbum.find({name: new RegExp(regexp,"i")}, function(err, found) {
    if(err) throw handleError(err);
    for(obj in found) {
      var name = found[obj].name;
      suggestions.suggestion.push(name);
      strings += name + "|";
    }


  });
}

Thank you 谢谢

That looks like Mongoosejs with MongoDB. 看起来像Mongoosejs和MongoDB。

IF it is, in that case, its not returning an object at "found". 在这种情况下,如果它不返回“找到”处的对象。 "found" is a collection that is an array already in which you would iterate through it like so: “找到”是一个已经是数组的集合,您可以在其中进行迭代,如下所示:

for(var i = 0; i < found.length; i++) {
   console.log(found[i]);
   // your code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM