From 2dd11bfda79088bc88a29f301855ccc2e10fd411 Mon Sep 17 00:00:00 2001 From: jianmingtu Date: Thu, 15 Apr 2021 12:50:15 -0700 Subject: [PATCH] #13 - search on both server and client. The Searching code works locally but failed on Heroku so the client side changed to use localhost:5000 instead. --- client/src/network.js | 7 ++++-- server/mongoDatabase.js | 51 +++++++++++++++++++---------------------- 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/client/src/network.js b/client/src/network.js index d311400..704654a 100644 --- a/client/src/network.js +++ b/client/src/network.js @@ -9,7 +9,8 @@ import { upload } from './s3' // export async function getPost({postId}) // export async function savePost({type, imageUrl, description}) -const BASE_API = "https://fathomless-lake-61399.herokuapp.com/api" +// const BASE_API = "https://fathomless-lake-61399.herokuapp.com/api" +const BASE_API = "http://localhost:5000/api" // Create an object to send it as a bearer token const authHeader = () => { return { Authorization: `Bearer ${localStorage.getItem('token')}` }} @@ -28,7 +29,9 @@ export async function getPosts() { export async function getSearchPosts({search}) { try { // Send the JWT in the header of the axios requests from the client - const result = await axios.get(`${BASE_API}/posts${ search ? `?search=${search}` : '' }`, { headers: authHeader() }) + const query = `${BASE_API}/posts${ search ? `?search=${search}` : '' }` + console.log('query+++++++++++',query) + const result = await axios.get(query, { headers: authHeader() }) console.log(result) return result.data } catch (error) { diff --git a/server/mongoDatabase.js b/server/mongoDatabase.js index 13879f3..04e723f 100644 --- a/server/mongoDatabase.js +++ b/server/mongoDatabase.js @@ -18,39 +18,34 @@ module.exports = async function() { async function getPosts(req) { - // try - // { + try + { - // const { limit = 100, skip = 0} = req - // const search = req.query?.search; - // const aggregateOptions = [] + const { limit = 100, skip = 0} = req + const search = req.query?.search; + const aggregateOptions = [] - // if (search) { - // aggregateOptions.push({ - // $match: { - // $expr: { - // $or: [ - // { $regexMatch: {input: '$pet_name', regex: new RegExp(`${search}`), options: "i" }}, - // { $regexMatch: {input: '$address_last_seen', regex: new RegExp(`${search}`), options: "i" }}, - // { $regexMatch: {input: '$user.username', regex: new RegExp(`${search}`), options: "i" }} - // ] - // } - // } - // }) - // } + if (search) { + aggregateOptions.push({ + $match: { + $expr: { + $or: [ + { $regexMatch: {input: '$pet_name', regex: new RegExp(`${search}`), options: "i" }}, + { $regexMatch: {input: '$address_last_seen', regex: new RegExp(`${search}`), options: "i" }}, + { $regexMatch: {input: '$user.username', regex: new RegExp(`${search}`), options: "i" }} + ] + } + } + }) + } - // return await db.collection('posts').aggregate(aggregateOptions).sort({ timestamp: -1, likes: 1}).skip(skip).limit(limit || 20).toArray() - // } catch(error) { - // return error - // } - - return await db.collection('posts') - .find({}) - .limit(20) - .sort({ "timestamp": -1 }) - .toArray() + return await db.collection('posts').aggregate(aggregateOptions).sort({ timestamp: -1, likes: 1}).skip(skip).limit(limit || 20).toArray() + } catch(error) { + return error + } + } async function createPost({ postDetails, user }) {