-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongostitch.html
67 lines (61 loc) · 2.26 KB
/
mongostitch.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<!doctype html>
<html lang="en">
<head>
<title> mongodb stictch</title>
<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.6.0/stitch.js"></script>
<!--
https://docs.mongodb.com/stitch/mongodb/actions/collection.find/
-->
<script>
const client = stitch.Stitch.initializeDefaultAppClient('smc-zmmqo');
const db = client.getServiceClient(stitch.RemoteMongoClient.factory, 'mongodb-atlas').db('test');
let retrieved;
function fetchAll() {
client.auth.loginWithCredential(new stitch.AnonymousCredential()).then(user =>
db.collection('signup').find({}, {
limit: 100
}).asArray()).then(docs => {
console.log("Found docs", docs)
retrieved = docs;
displayComments(docs);
console.log("[MongoDB Stitch] Connected to Stitch")
}).catch(err => {
console.error(err)
});
}
function addProfile() {
const newprofile = document.getElementById("new_profile");
console.log("add profile", client.auth.user.id)
db.collection("signup")
.insertOne({
owner_id: client.auth.user.id,
profile: newprofile.value
})
.then();
new_profile.value = "";
}
function displayComments(retrieved){
let commentsHTML = "<h2>Who said:</h2>";
for (let i = 0; i < retrieved.length; i++){
commentsHTML += retrieved[i].profile + "<br>";
}
document.getElementById("docsDisplay").innerHTML=commentsHTML;
}
</script>
</head>
<body>
<h3> This is a great blog post </h3 >
<div id = "content">
I like to write about technology because I want to get on the
front page of hacker news.
</div>
<hr>
Add comment:
<input id = "new_profile">
<input type = "submit" value="Add" onClick = "addProfile()">
<br>
<input type = "submit" value="Fetch" onClick = "fetchAll()">
<div id="docsDisplay">
</div>
</body>
</html>