0

我对 Mongodb 很陌生,并尝试使用 JS 创建简单的 HTML,其中可以在云 Atlas 上的 Mongodb 数据库中添加和删除用户。添加用户工作正常。但由于某种原因,无法识别删除功能。app id 是 --> "facebookclone- tlwvi" 我将分开 js 代码,所以下面是现在的代码:

<!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>
4

1 回答 1

0

MongoDBremove方法已弃用。我将其更改为itemsCollection.deleteOne代替,remove尽管它确实出现了其他错误,但它能够解决您remove未定义的问题。

使用deleteOne导致 HTTP 403(禁止)错误。

于 2018-04-13T18:37:23.987 回答