added delete function

This commit is contained in:
Daniel
2022-04-26 10:50:42 +04:30
parent c757b2bfa6
commit e49ade7462
6 changed files with 99 additions and 31 deletions
+27 -1
View File
@@ -1,6 +1,6 @@
const express = require('express');
const app = express();
const { MongoClient } = require('mongodb');
const { MongoClient, ObjectId } = require('mongodb');
const phantom = require('phantom');
const port = process.env.PORT || 3001;
@@ -23,6 +23,14 @@ app.post('/post', async (req, res) => {
insertDoc(req.body);
});
app.delete('/delete', async (req, res) => {
const id = req.body.id.toString();
await deleteDoc(id);
res.send(`Bookmark with ObjectId "${id}" deleted.`);
});
async function insertDoc(doc) {
try {
await client.connect();
@@ -61,6 +69,24 @@ async function getDoc() {
}
}
async function deleteDoc(doc) {
try {
await client.connect();
const database = client.db("sample_db");
const list = database.collection("list");
const result = await list.deleteOne({"_id": ObjectId(doc)});
}
catch(err) {
console.log(err);
}
finally {
await client.close();
}
}
async function getTitle(url) {
const instance = await phantom.create();
const page = await instance.createPage();