-
Type:
Bug
-
Resolution: Works as Designed
-
Priority:
Minor - P4
-
Affects Version/s: None
-
Component/s: None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
I try to use the 'search text' technique in MongoDB with Sails Framework however when I try to get the request param from the front end and pass it into the query an error was thrown like this:
{
"originalError": {
"name": "MongoError",
"message": "\"$search\" had the wrong type. Expected string, found regex",
"ok": 0,
"errmsg": "\"$search\" had the wrong type. Expected string, found regex",
"code": 14,
"codeName": "TypeMismatch"
},
"_e": {
"name": "MongoError",
"message": "\"$search\" had the wrong type. Expected string, found regex",
"ok": 0,
"errmsg": "\"$search\" had the wrong type. Expected string, found regex",
"code": 14,
"codeName": "TypeMismatch"
},
"rawStack": "MongoError: \"$search\" had the wrong type. Expected string, found regex\n at Function.MongoError.create (D:\\Projects\\fifo\\abc-web\\node_modules\\mongodb-core\\lib\\error.js:31:11)\n at queryCallback (D:\\Projects\\fifo\\abc-web\\node_modules\\mongodb-core\\lib\\cursor.js:200:36)\n at Callbacks.emit (D:\\Projects\\fifo\\abc-web\\node_modules\\mongodb-core\\lib\\topologies\\server.js:119:3)\n at Connection.messageHandler (D:\\Projects\\fifo\\abc-web\\node_modules\\mongodb-core\\lib\\topologies\\server.js:371:23)\n at Socket.<anonymous> (D:\\Projects\\fifo\\abc-web\\node_modules\\mongodb-core\\lib\\connection\\connection.js:301:22)\n at emitOne (events.js:96:13)\n at Socket.emit (events.js:188:7)\n at readableAddChunk (_stream_readable.js:176:18)\n at Socket.Readable.push (_stream_readable.js:134:10)\n at TCP.onread (net.js:548:20)",
"details": "Details: MongoError: \"$search\" had the wrong type. Expected string, found regex\n",
"isOperational": true,
"message": "[Error (E_UNKNOWN) Encountered an unexpected error] Details: MongoError: \"$search\" had the wrong type. Expected string, found regex\n",
"stack": "Error (E_UNKNOWN) :: Encountered an unexpected error\nMongoError: \"$search\" had the wrong type. Expected string, found regex\n at Function.MongoError.create (D:\\Projects\\fifo\\abc-web\\node_modules\\mongodb-core\\lib\\error.js:31:11)\n at queryCallback (D:\\Projects\\fifo\\abc-web\\node_modules\\mongodb-core\\lib\\cursor.js:200:36)\n at Callbacks.emit (D:\\Projects\\fifo\\abc-web\\node_modules\\mongodb-core\\lib\\topologies\\server.js:119:3)\n at Connection.messageHandler (D:\\Projects\\fifo\\abc-web\\node_modules\\mongodb-core\\lib\\topologies\\server.js:371:23)\n at Socket.<anonymous> (D:\\Projects\\fifo\\abc-web\\node_modules\\mongodb-core\\lib\\connection\\connection.js:301:22)\n at emitOne (events.js:96:13)\n at Socket.emit (events.js:188:7)\n at readableAddChunk (_stream_readable.js:176:18)\n at Socket.Readable.push (_stream_readable.js:134:10)\n at TCP.onread (net.js:548:20)"
}
Here is my block of code:
let query = { 'is_new': true };
let search = req.param('search', null);
if (search) {
query['$text'] = { '$search': search }; // After this line of code the 'query' object will be { 'is_new': true, '$text': { '$search': '<VALUE>' }}
}
Products.native(function (err, collection) {
if (err) {
...
}
return collection.find(query).toArray(function (err, results) {
...
}
}
Surprisingly, if I hard code the 'search' variable with a specific value it works well:
Products.native(function (err, collection) { if (err) { ... } return collection.find({ 'is_new': true, '$text': { '$search': 'ble' }}).toArray(function (err, results) { ... } }
References:
+ https://sailsjs.com/documentation/reference/waterline-orm/models/native
+ https://docs.mongodb.com/v3.4/text-search/