I have an aggregation function that works fine when using postman. When I test using supertest I get:
MongoServerError: PlanExecutor error during aggregation :: caused by :: ReferenceError: cov_2nu0vpmwxb is not defined :
@:3:13
at MessageStream.messageHandler (/Users/Ryan.Moore/fls/fls/apps/chops-api/src/node_modules/mongoose/node_modules/mongodb/src/cmap/connection.ts:753:20)
at MessageStream.emit (node:events:520:28)
at processIncomingData (/Users/Ryan.Moore/fls/fls/apps/chops-api/src/node_modules/mongoose/node_modules/mongodb/src/cmap/message_stream.ts:168:12)
at MessageStream._write (/Users/Ryan.Moore/fls/fls/apps/chops-api/src/node_modules/mongoose/node_modules/mongodb/src/cmap/message_stream.ts:65:5)
at writeOrBuffer (node:internal/streams/writable:390:12)
at _write (node:internal/streams/writable:331:10)
at MessageStream.Writable.write (node:internal/streams/writable:335:10)
at Socket.ondata (node:internal/streams/readable:777:22)
at Socket.emit (node:events:520:28)
at addChunk (node:internal/streams/readable:324:12) {
ok: 0,
code: 139,
codeName: 'JSInterpreterFailure'
}
The aggregation function is:
/** extractResponseFields - extracts the tag from the response field into a new field
* response will look like:
* "response": "Let me get someone to talk to you about your account.<metric name=\"Missing_accounts\"
* stage=\"Handover_to_agent_IB\" /> <metric name=\"Speak_to_agent_DB\" stage=\"entrypointIB\" />
* <pause wait=2000 /><metric name=\"Speak to agent_DB\" stage=\"context_set_IB\" />
* <pause wait=2000 />I'm handing you over to one of my team who'll be happy to help.
* They'll be with you as soon as they can.<metric name=\"DB_Speak_to_agent\"
* stage=\"in_hours_handover_IB\" /> <pause wait=2000 /><handover skill=\"DS_Banking_IB\" />"
*
* So we need to extract and store each of the values of the tag in a new field e.g.
* metrics: [ "Missing_accounts", "Speak_to_agent_DB", "Speak_to_agent_DB", "DB_Speak_to_agent" ]
* stages: [ "Handover_to_agent_IB", "entrypointIB", "context_set_IB", "in_hours_handover_IB" ]
* handoverSkills: [ "DS_General_IB" ]
*
* 1. match all records that have '<tag>' in the response field - pass in like stage="
* 2. create an additional field to hold the tag data
* 3. merge the new field data into the original collection
* @param {*} tag - tag to find in response field.
* @param {*} fieldName - name of field to create.
* @returns {Object} A distinct list of the values for this field sorted alphabetically.
* @returns {Integer} The count of items in the list.
*/
const extractResponseFields = async (tag, fieldName) => {
console.log('extractResponseFields:', tag, fieldName, tag.length)
const hs = await Conversations.aggregate([
{ // stage 1 - match all records than have '<tag>' in the response field
$match: {
response: {
$regex: tag
}
}
},
{ // stage 2 - create an additional field to hold the tag data
$set: {
[fieldName]:
{
$function:
{
body: function (response, tag) {
// split the function by the tag - creates an array
const arr = response.split(tag)
const tags = []
for (let i = 1; i < arr.length; i++) { // ignore first element '0' as it is the text preceding tag
// take the text up until the next "
tags.push(arr[i].split('"')[0])
}
return tags
},
args: ['$response', tag], // $response comes from collection, tag is the variable passed
lang: 'js'
}
}
}
}
,
{ // stage 3 - merge the new field data into the original collection
$merge: { into: { db: 'chops', coll: 'conversations' }, on: '_id', whenMatched: 'merge', whenNotMatched: 'insert' }
}
])
// return [hs, hs.length]
//console.log(hs, hs.length)
// return a distinct collection of the newly created field
const results = await Conversations.distinct(fieldName).sort()
return [results, results.length]
}
test is…
it('GET ' + endpoint + ' - Should return 90 distinct values from stages.', async () => {
const response = await request(app).get(endpoint)
.query({
'metric': 'stage=\"',
'fieldname': 'stages'
}).send()
expect(response.statusCode).toBe(200)
expect(response.body.page * 1).toBe(1)
expect(response.body.resultsPerPage * 1).toBe(90)
expect(response.body.totalCount * 1).toBe(90)
// sample data
expect(response.body.extractResponseFields[0]).toBe('1_Ask_another_way')
expect(response.body.extractResponseFields[7]).toBe('Borrowing')
})
Every time I run I get the same object reference of ‘cov_2nu0vpmwxb’ in the error
How do I find out what ‘cov_2nu0vpmwxb’ is?