Last active
August 29, 2015 14:03
-
-
Save dcs619/95f9ae5f77a309d2a2f3 to your computer and use it in GitHub Desktop.
isQueryInclusive scoping issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # src/server/app/methods/stash.coffee | |
| ### | |
| Saves the original search result to the DB | |
| #### | |
| Query = require('../models/search').Query | |
| Results = require('../models/search').Results | |
| # number of milliseconds, currently two hours | |
| CacheCutoff = 1000 * 60 * 60 * 2 | |
| module.exports = (originalQuery, responses ) -> | |
| # extract query string | |
| queryString = originalQuery.q | |
| queryHost = originalQuery.siteSearch | |
| # return query if this has been searched on this host | |
| Query.findOne({ | |
| 'queryString': queryString, | |
| 'host': queryHost | |
| }, (error, query) -> | |
| return error if error | |
| updateNeeded = false | |
| if query? | |
| console.log( 'retrieving') | |
| # check timestamp is less than the cutoff | |
| currentDate = new Date() | |
| if ( currentDate - query.updated_at ) > CacheCutoff | |
| query.updated_at = currentDate | |
| # else | |
| # console.log( 'returning' ) | |
| # return | |
| else | |
| # create new query | |
| console.log( 'creating' ) | |
| query = new Query( queryString: queryString, host: queryHost ) | |
| # save the current or new query | |
| console.log( 'saving query' ) | |
| query.save( ( error ) -> | |
| return error if error | |
| ) | |
| ### | |
| this doesn't keep the value of isQueryInclusive | |
| throughout the for loop | |
| ### | |
| # create results & associate with query | |
| for response in responses | |
| console.log( response.queries.request[0] ) | |
| isQueryInclusive = response.queries.request[0].siteSearchFilter is 'i' | |
| Results.findOne({ | |
| '_query': query._id | |
| 'isInclusive': isQueryInclusive | |
| }, (error, result) -> | |
| console.log( response.queries.request[0] ) | |
| localInclusive = response.queries.request[0].siteSearchFilter is 'i' | |
| console.log( 'after find', localInclusive ) | |
| return error if error | |
| associateResult = false | |
| unless result | |
| console.log( 'creating result' ) | |
| console.log( response.queries.request[0].siteSearchFilter is 'i' ) | |
| associateResult = true | |
| result = new Results({ | |
| _query : query._id, | |
| isInclusive: localInclusive | |
| }) | |
| result.request = response | |
| result.items = response.items | |
| result.pagination = response.queries | |
| # save current or new result | |
| console.log( 'saving result' ) | |
| result.save( (error) -> | |
| return error if error | |
| ) | |
| # make sure the new result gets associated with the query | |
| if associateResult | |
| query._results.push( result ) | |
| query.save() | |
| ) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment