Fetching test data as string and trying to PARSE that string into object to use in mongo DB as query to fetch values from DB, getting error in JSON.parse(query)
From MongoDB getting string : query= “{ “$and”: [{ “status”: { “$ne”: ‘Completed’ } }, { “status”: { “$ne”: ‘Canceled’ } }] };// as string from DB
` query = query.replace(/\/gi, '');
query = query.replace(/'/gi, '"');
//get branch request id from db
// query 1 hardcoded works fine
const query1 = { $and: [{ status: { $ne: 'Completed' } }, { status: { $ne: 'Canceled' } }] };
console.log('DB query ----- ', query);
const dbRequestList = await DB.getDocumentsFromDb(
CONSTANTS.DB_NAME.ONBOARDING,
CONSTANTS.COLLECTION_NAME.BRANCH_REQUEST,
JSON.parse(query),
);`
//SyntaxError: Unexpected token { in JSON at position 61
//if dont wright “query = query.replace(/’/gi, ‘”‘);” then throw error as— SyntaxError: Unexpected token ‘ in JSON at position 32
enter image description here
BHakti K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.