I am creating a typeahead search to find tags from a mongodb collection. Im not sure how to return the results (for every keystroke) so that it will dropdown with options and then populate the solution for the tag.
I have followed the wikipedia example found here: https://ng-bootstrap.github.io/#/components/typeahead/examples
I have a service which I pass a search term into and then I run a find in the searchDB.js in order to return back the results.
When I return the reponse all I get is a whole lot of code in my terminal - starting with ServerResponse {....
I just cant get it to return the results.
// This is my search.js
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const { Schema } = mongoose;
// create a schema
const searchSchema = new Schema({
tag: String,
solutions: Array
});
const Search = mongoose.model('Search', searchSchema);
module.exports = Search;
// This is in my searchDB.js
const express = require('express');
const Search = require('../models/search');
const router = express.Router();
/* GET api listing. */
router.get('/:text', (req, res) => {
var q = req.params.text;
Search.find({
$text: {
$search: q
}
}, {
_id: 0,
__v: 0
}, function (err, data) {
res.json(data)
});
});
module.exports = router;
// This is in my search.service.ts
search(term: string) {
if (term === '') {
return of([]);
}
return this.http.get(`/db/search/${term}`).pipe(
map(response => response[1])
);
}
// This is in my search.compponent.ts
search = (text$: Observable<string>) =>
text$.pipe(
debounceTime(300),
distinctUntilChanged(),
tap(() => this.searching = true),
switchMap(term =>
this._service.search(term).pipe(
tap((searchResults) => { console.log(searchResults); this.searchFailed = false; }),
catchError(() => {
this.searchFailed = true;
return of([]);
}))
),
tap((searchResults) => { console.log(searchResults); this.searching = false; })
)
// This is my search.component.html
<input id="search" name="search" [ngbTypeahead]="search" type="text" placeholder="Search for solutions..." class="form-control-search-bar form-control-underlined amcs-search">
// This is what my mongo db collection looks like
{
"tag" : "truck",
"solutions" : [
{
"id" : 1,
"title" : "where is the truck"
},
{
"id" : 1234,
"title" : "What is a rubbish truck"
},
{
"id" : 12345,
"title" : "Do vans or lorrys or trucks take bins"
}
]
}
{
"tag" : "bin",
"solutions" : [
{
"id" : 1230,
"title" : "Do vans or lorrys or trucks take bins"
}
]
}
When a user types "tr" into the search bar I want the dropdown display all the solution titles that have the tag starting with "tr" - in this case truck. Or if it was "b" then the tag bin would show its solution titles in the dropdown. The solution ID will be used as a link so that when the title is clicked it will be directed to that id.