I am attempting to write a function to find if a document within a mongoose database exists before submitting a query. However, it keeps saying that the document does not exist, even though it does.
/**
* Determines if a Journal already exists (via ISSN numer)
* @param {string} data - The ISSN number of the Journal to be checked
* @return {boolean} - True = journal exists, false it doesnt exist.
*/
async function getJournalByISSN(data) {
const issn = String(data);
const docCount = await Journal.countDocuments().or([
{issn_electronic: issn},
{issn_print: issn}]);
let value = false;
console.log(docCount, value);
if (docCount != 0) value = true;
return value;
}
The console.log of the data given returns a correctly formatted ISSN. When I debug the mongoose query it looks well-formed.
console.log
journals.countDocuments {"$or":[{"issn_electronic":"1234-1234"},{"issn_print":"1234-1234"}]} {}
When manually specifying the search field, ie
{issn_electronic: '1234-1234'}, {issn_print: '1234-1234'}]}
It returns the correct document count value.
Interestingly copying and pasting the debug of mongoose shows that there has been blank spaces injected into the string
eg
journals.countDocuments {"$or":[{"issn_electronic":"1234- 1234"},{"issn_print":"1234- 1234"}]} {}
This was an issue with hidden characters
String has hidden characters, Can't able to verify ,
By replacing the data string via it solved the issue
data = data.replace(/[\u200c\u200b]/g, '')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.