How to check if object exists in mLab using _id?

核能气质少年 提交于 2019-12-24 19:14:23

问题


I am working on an angular application with a node.js backend where my architecture goes like this:

front-end => angular.service => node backend => mLab DB

Currently, I'm trying to push an object into the DB provided that it does not exist yet. If it already does, it should update. This function would be accessible via a button from the cards in my front-end.

to give a clearer understanding here's some of my code.

front-end: admin-edit-home.component.html

<a mdbBtn class='btn btn-md btn-primary' mdbWavesEffect (click)="addCard()">Add</a>

the code above is a button where I can add a card to the interface. The TS below shows how the code works.

front-end: admin-edit-home.component.ts

addCard() {
  this.carousels.push(this.carousels.length);
}

To give an explanation of the TS code, 'carousels' is an array that I use to do an *ngFor loop in my HTML wherein it presents the data in a card format. It is declared as:

carousels: any = [];

So in pushing a length to the 'carousels' array, it present an empty card with no collected data but still possessing the HTML elements from the original card which contains the supposed update function that I would like to have.

My problem is, how do I do the checking of the object existence from the back-end and present the results back to the front-end? I have tried this,

back-end: api.js

router.route('/carousel/update/:id').put(function(req,res) {
   var data = req.body;
   const myquery = { _id: ObjectId(req.params.id) };

db.collection('home').updateOne(myquery, {
   $set: {
       "header" : data.header,
       "subheader" : data.subheader,
       "img" : data.img
   }

  })
  if (myquery === -1) {
  arr.push(obj);
  } else {
      arr[myquery] = obj
  }
}

I know my back-end code is wrong and non-functional but I just wanted to let you guys have a visualisation of what my logic is trying to achieve.

Furthermore, this back-end code should now be accessible by my angular service through this chunk of code below:

home.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable ({
  providedIn: 'root'
})

export class HomeService {

   constructor(private http: HttpClient) {}

   updateCard(id: string, header: string, subheader: string, img: string) {
       var json = {id: id, header: header, subheader: subheader, img: img}
       return this.http.put<any[]>(`./api/carousel/update/${id}`, json);  
   }
}

After trying these things, to sum up my problem in a more concise manner, I need to check from the database if the object is already existing via ObjectId and then update it through my input fields but if not, the updateCard() should create another object in my database. I hope to get help!

EDIT

 router.route('/carousel/update/:id').put(function (req, res) {
     var data = req.body;
     const myquery = { "_id": ObjectId };
    // console.log('header: ' + data.header + " id: " + data.id)
    console.log(req.params)
     db.collection("home").updateMany(myquery, {
         $set: {
             "img" : data.img,
             "header" : data.header,
             "subheader": data.subheader
         }
     }, (err, results) => {
        res.status(200).json({status: "ok"})
     })

 })

This is the new api.js. Please refer.

来源:https://stackoverflow.com/questions/58315104/how-to-check-if-object-exists-in-mlab-using-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!