Search code examples
javascriptmongodbmongodb-atlasmongodb-stitch

Uncaught (in promise) TypeError: itemsCollection.remove is not a function at stClient.login.then


I am quite new with Mongodb and trying to create simple HTML with JS where one can add and delete user from Mongodb database on cloud Atlas. Adding user works fine. But for some reason, remove function is not recognized. app id is --> "facebookclone- tlwvi" I will separete js code so below is the code for now:

<!DOCTYPE html>
<html>
<head>
<title>myFb</title>

 <link rel="stylesheet" type="text/css" href="style1.css">
<script src="https://s3.amazonaws.com/stitch- 
sdks/js/library/v3/stable/stitch.min.js"></script>

<script>
let db;
let itemsCollection;
let stClient;
let clientPromise = stitch.StitchClientFactory.create('facebookclone- 
tlwvi');


function onLoadConnectDB(){
    clientPromise.then(stitchClient=>{
        stClient=stitchClient;
        db = stClient.service('mongodb', 'mongodb-atlas').db('FbUsers');
        itemsCollection=db.collection("ClonedFbUsers");
    });

}

 function addUser(){
 var n= prompt("Your username: ")
const userId = stClient.authedId();
stClient.login().then(()=>
    itemsCollection.insertOne({ owner_id: stClient.authedId(), userName : n 
, profilePhoto: "NULL", photos: ["NULL"],comments: 
 [{msg:"NULL",time:"NULL",like:0}] })
    ).then(() => itemsCollection.find({}).execute())
  .then(docs =>
  docs.forEach((doc, index) =>
    console.log(`${index}: ${JSON.stringify(doc)}`)
    )
  );
alert("added");
}

 function deleteUser(){

var d= prompt("Username to delete: ");
const userId = stClient.authedId();

stClient.login().then(()=>
    itemsCollection.remove({ userName: {$eq: d} })
    ).then(() => itemsCollection.find({}).execute())
.then(docs =>
  docs.forEach((doc, index) =>
    console.log(`${index}: ${JSON.stringify(doc)}`)
    )
  );
alert("User "+d+ " deleted.");


}

</script>
 </head>


 <body onload="onLoadConnectDB()">


<p>Hello My App</p>

<div id="wlcom" align="center">
<button name="adding" onclick="addUser()">Add User</button><br>
<button name="deleting" onclick="deleteUser()">Delete User</button> <br>
<button name="logging">Login</button><br>
 </div>

</body>
</html>

Solution

  • The MongoDB remove method is deprecated. I changed this to itemsCollection.deleteOne instead of remove and although it did get other errors, it was able to get past your issue of remove being undefined.

    Using deleteOne resulted in an HTTP 403 (Forbidden) error.