Return the data from beforeFind trigger and stop the Find query


I am integrating ElasticSearch to Parse Server for caching and searching capabilities.

I’ll cache data of few specific classes into the ElasticSearch. When a find query is executed on one of those classes then a check is first made to see if the data is cached on the Elastic server. If it is then return it. If not, get it from the parse database.

I want to complete this process by using the beforeFind trigger of a class to check in the ElasticSearch.

For example in Vendor class,

// main.js
const es = require("./elasticsearch.js");
Parse.Cloud.beforeFind(Parse.Vendor, async (request) => {
  // Call Elastic function
  es.esSearch(request, (res) => {
    // if response has data then stops the find query return the data
    // If not, continue to find query

// elasticsearch.js
exports.esSearch = async function (req) {
   // returns data

So, How to return data from the beforeFind trigger without throwing error and also stop executing of find query?

Similar to this feature
Implementing Redis Cache functionality for Parse-server · Issue #4750 · parse-community/parse-server · GitHub


I believe there is no way to do that using the beforeFind trigger. I’d try to do that using cloud code functions.

Hi, Ender,

may I ask how did you implement the ElasticSearch in to your cloud function?

I am learning with Parse Server on Back4App and my guess is I would need to ask them to install some adapter or package to access the functionality GitHub/elasticsearch-js? Or did you simply wrote your own function for HTTP requests?

Thank you! Lukas

const elastic = require("@elastic/elasticsearch")

Paste this to your cloud code. I believe back4app install neccessary packages during build.

1 Like

Hi Uzaysan,

I tried following in my elastic.js:

const elastic = require("@elastic/elasticsearch")
const esClient = new elastic({ node: “” })

but server returns an error:

Error loading your cloud code:
TypeError: elastic is not a constructor
at Object. (/usr/src/app/data/cloud/functions/elastic.js:2:18)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:1153:10)
at Module.load (internal/modules/cjs/loader.js:977:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Module.require (internal/modules/cjs/loader.js:1019:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object. (/usr/src/app/data/cloud/triggers/profile.js:1:17)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:1153:10)

I also uploaded file package.json to the cloud folder where main.js is sitting with following:

“dependencies”: {
@elastic/elasticsearch”: “*”

I am trying to reproduce simple index functions here: documentation

try this:

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
  host: 'localhost:9200',
  log: 'trace',
  apiVersion: '7.2', // use the same version of your Elasticsearch instance

It seems that missing ; was a problem and then trying to figure out what is going on I changed Client to elastic what is then not defined in the dependency. This is working now:

const { Client } = require('@elastic/elasticsearch');
const esClient = new Client({ node: 'http://xxxx:9200/' });

Oh, how I love javaScript… :smiley:

1 Like

Hi! We also need this. In our case, the Parse collection contains cached data, and the main data source is remote.

We need to check the remote source, update Parse Collection and return the result.

How does the Parse team would feel about being able to return a Parse Object collection from BeforeFind?

That makes sense for me, but I’d rather probably use a cloud code function for that.

1 Like