Currently, I am facing an issue while trying to insert a document into my MongoDB database. Whenever I call insert() on a collection, it throws an error stating (FacilitatorJS.js:26 Uncaught (in promise) TypeError: db.collection(...).insert is not a function at db.collection.find.execute.then.result (FacilitatorJS.js:26)
If anyone could assist me with this problem, it would be greatly appreciated. I have also attempted using insertOne() but unfortunately, that hasn't worked either.
Below is the code snippet:
// Set up server connection
const clientPromise = stitch.StitchClientFactory.create('CODE');
var client;
var db;
let stitchClient;
/**
* Connects to DB and can retrieve a facilitator's existing id or create a new one if name doesn't exist in db.
* @param {dict} name - name to find or insert
*/
function connect(name){
clientPromise.then(stitchClient =>{
client = stitchClient;
db = client.service('mongodb', 'mongodb-atlas').db('budgetopolis');
return client.login().then(getFacilitatorID(name))
});
}
function getFacilitatorID(name){
db.collection('Facilitators').find(name).execute().then(result => {
if(result.length == 0){
// Create a facilitator object in db if it doesn't exist
db.collection("Facilitators").insert(name).execute().then(result1 => {
// Get the newly added Facilitator's ID
console.log('created new user')
db.collection('Facilitators').find(name).execute().then(result2 => {
var fullId = result[0]['_id']
Facilitator.session_id = fullId.toString().slice(-4)
updateMessageBar("Your new session id is: " + Facilitator.session_id)
});
});
} else {
// Working Code Here
}