added delete function
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user