[Vuejs]-I need help to get a simple query for MongoDB (SQL equivalent: "select speed from values")

0👍

In the screenshot you show the projection is values.speed.value, but in your endpoint you have:

const select = {};
select[`${val}.value`] = 1;

Which would evaluate to speed.value. To mimic what you have on the MongoDB console this should be:

const select = {};
select[`values.${val}.value`] = 1;

0👍

So, I changed my Frondend (vue) to spred all Values. I maked some Helper Functions:


  • CheckForChangedObj
    • check two object for changes
  • updateObj
    • copy all data from object into another

export function CheckForChangedObj(targetObject, obj) {
  if (targetObject === typeof undefined || targetObject == {}) {
    return true
  }

  if (JSON.stringify(targetObject) !== JSON.stringify(obj)) {
    return true
  }

  return false
}


export function updateObj(targetObject, obj) {
  let keys = Object.keys(obj)

  for (let k in keys) {
    try {
      let key = keys[k]
 
      if (!targetObject.hasOwnProperty(key)) {
        //define target, if not exist
        targetObject[key] = {}
      }

      //Workaround for References
      if (obj[key].hasOwnProperty("__v_isRef")) {
   
        if (targetObject[key].hasOwnProperty("__v_isRef")) {
          targetObject[key].value = obj[key].value
        } else {
          targetObject[key] = obj[key].value
        }
      } else {
        //Source i a Norm Value
        //check for an Object inside, then go in...
        if ("object" === typeof obj[key] && !Array.isArray(obj[key])) {
          updateObj(targetObject[key], obj[key]) // recurse
        } else {
          //not else-if!!! __v_isRef: true
          if (targetObject[key].hasOwnProperty("__v_isRef")) {
            targetObject[key].value = obj[key]
          } else {
            targetObject[key] = obj[key]
          }
        }
      }
    } catch (error) {
      console.log(
        "key:",
        keys[k],
        "Error:",
        error
      )
    }
  }
}


Then spreading…

import {updateObj,CheckForChangedObj } from "../tools/tools"

beforeMount() {
     this.getAllValuesAPI()
    setInterval(() => {
      this.checkForChanges()
      // ml("TABLE", this.values)
    }, 10000)
  },

setup() {
let prevValues = {}
let values = {
      speed: {
        id:1,
        type: SettingType.Slider,
        value: ref(0)
      }
      //...
}

      function checkForChanges() {
        //intervaled function to get changes from API
          let changed = CheckForChangedObj(this.values, this.prevValues)
          if (changed) {
            updateObj(this.prevValues, this.values)
            setValuesToAPI()
          }
          else{
            getAllValuesFromAPI()
          }
      }

      async function  getAllValuesFromAPI() {
        //get ALL Settings-Data from API, and put them in values
        const res = await axios.get(`settings/getAll`)
        return  updateObj(this.values, res.data[0].values) u
      }
}


When you have a better solution, please help…

Leave a comment