Skip to content

Commit

Permalink
#13 - search on both server and client. The Searching code works loca…
Browse files Browse the repository at this point in the history
…lly but failed on Heroku so the client side changed to use localhost:5000 instead.
  • Loading branch information
jianmingtu committed Apr 15, 2021
1 parent 810f151 commit 2dd11bf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
7 changes: 5 additions & 2 deletions client/src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')}` }}
Expand All @@ -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) {
Expand Down
51 changes: 23 additions & 28 deletions server/mongoDatabase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand Down

0 comments on commit 2dd11bf

Please sign in to comment.