Reputation: 21
I am currently trying to insert a document to my mongodb but when I call insert() on a collection, an error is produced saying (FacilitatorJS.js:26 Uncaught (in promise) TypeError: db.collection(...).insert is not a function at db.collection.find.execute.then.result (FacilitatorJS.js:26) at )
Could anyone help with this? I've tried insertOne() with no luck either.
Here's the code below
//prep 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 doesnt 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 doesn't exist
db.collection("Facilitators").insert(name).execute().then(result1 => {
//now 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
Upvotes: 2
Views: 1923
Reputation: 18845
TypeError: db.collection(...).insert is not a function at db.collection.find.execute.then.result (FacilitatorJS.js:26) at )
There is no function called insert()
for a collection on the stitch-sdk JavaScript. You should use insertOne()
instead. If you have tried with insertOne()
, the error line probably differs.
I would suggest to follow one of the MongoDB Tutorials: Stitch to get started.
Upvotes: 2