Make changes to an Object in the Parse database

For my current project, I am developing an Ionic app with Parse.com as the backend. My goal is to save an item to an existing Object in the database. If the item already exists, I want to update it instead of creating a new entry every time. I have been using the .save method from Parse documentation, but it keeps generating new rows.

To query for the existing ID, I use the following code:

var ExistingId = Parse.Object.extend("Exercises");
var query = new Parse.Query(ExistingId);

query.equalTo("exerciseName", $scope.currentExcercise);
query.find({
  success: function(results) {
  
    for (var i = 0; i < results.length; i++) { 
      var object = results[i];
      
      var ExistingIdNumber = object.id;
    }
  },
  error: function(error) {
    alert("Error: " + error.code + " " + error.message);
  }
});

This is the code I use to create or save rows in the Exercises Object:

$scope.saveToParse = function(exercise) {

var Exercises = Parse.Object.extend("Exercises");
var exercises = new Exercises();

exercises.set({exerciseName: exercise.ExerciseName,
          exerciseID: exercise.ExerciseID,
          exerciseDescription: exercise.Description,
          sets: exercise.Sets,
          reps: exercise.Reps,
          resistance: exercise.Resistance,
          tempo: exercise.Tempo,
          images: $scope.images})

exercises.save(null, {
  success: function(exercises) {
    
    alert('Exercise saved with objectId: ' + exercises.id);
  },
  error: function(exercises, error) {
    
    alert('Failed to create new object, with error code: ' + error.message);
  }
});

}

I am looking for a way to combine these two functions to automatically check for existing values and either create or update them.

Best regards, Simon

Answer №1

Success! This method is functioning properly.

$scope.saveToParse = function(data) {

var Items = Parse.Object.extend("Items");
var item = new Items();
item.id = $scope.ExistingItemID;
console.log($scope.ExistingItemID);

item.fetch()
.then(function(){
      item.set({itemName: data.itemName,
          itemID: data.itemID,
          itemDescription: data.description,
          quantity: data.quantity,
          price: data.price,
          category: data.category,
          images: $scope.images})

      return item.save();
       $state.go('app.items', {clear: true});

})
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Extract image file name and date information (day, month, year) from an HTML form using Angular

The content of the register.component.ts file can be found below: import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-register', templateUrl: './register.component.html', styleUrls: [&apo ...

Incorporate the feedback received from the user in the previous trial as a prompt for the next

I am currently working on developing an experiment using the JSPsych JavaScript package. The main goal of my experiment is for participants to input a letter in a survey-text trial, and then have that same letter presented back to them as a stimulus in the ...

Managing multiple changes in input values within an object

Looking to update multiple input field values using the handleChange() method with a starter object that includes its own properties. The goal is to assign input field values to corresponding properties within the starter object. However, the current imple ...

Oops! Looks like the file or directory at '/home/arpit/.steampath' couldn't be found

Whenever I attempt to execute npm start, I encounter the following error: [Error: ENOENT: no such file or directory, stat '/home/arpit/.steampath']. Despite trying various solutions like removing and reinstalling node modules and NPM, as well as ...

Adding properties with strings as identifiers to classes in TypeScript: A step-by-step guide

I am working with a list of string values that represent the identifiers of fields I need to add to a class. For instance: Consider the following string array: let stringArr = ['player1score', 'player2score', 'player3score' ...

Encountering an issue while attempting to retrieve information from Vuex store

I recently encountered an issue while trying to store my water data in Vuex. I followed the implementation below, but when I attempted to access my data array, it did not show up as expected. const store = new Vuex.Store({ state: { categories: ...

Error: JSON unexpectedly received token "a" at the beginning while parsing at position 0 (anonymous)

There are many questions regarding this topic, but I am genuinely confused about this error... I have developed a login page and utilized AJAX for the POST Request. The issue arises when I use the following code: $.ajax({ url:'../ajax/checklogin.ph ...

I have instructed angular's $stateProvider to include '/#' in my routes

I'm currently using $stateProvider to manage my routes in Angular 1 and I'm puzzled by the fact that all of my routes have a /# prefix before them. This is causing issues when I try to test these routes in Postman as they result in a 404 error. I ...

A step-by-step guide on showing and printing an array using jQuery

My question is about handling an array. var arr = [1,2,3,4,5,6,7,8,9,10]; How can I display all the items of the array using an alert box? I attempted to use: alert(arr); but it did not show anything. Edit: I would like to display the array similar to ...

Sequelizejs establishes a belongsToMany relation with a specified otherKey

I am currently developing an app centered around songs and artists, and here is the database schema I have designed: Each Song can have multiple Artists associated with it, and each Artist can be linked to several Songs as well. This establishes a many-to ...

Is there a way for me to implement a submission counter for forms?

Hello! I am currently developing a pledge page and I have a question about incorporating a counter that updates each time a user submits the form. I would like it to display something similar to this: [ Thank you for committing to take the pledge. Curren ...

Removing information from the app.component.html file in Angular 6 and reflecting those updates in the view

I currently have a service that retrieves a list of data and displays it within the app.component.html. Below is the code snippet used to display the data: <ul> <li *ngFor="let data of retrieveData"> {{ data.id }} - {{data.title} ...

Attempting to construct an 'or' query that relies on the combination of two distinct joined tables

Currently, I am facing a challenge with selecting rows from a PostgreSQL database through Sequelize. I need to retrieve data where the ID exists in either joined table 1 or joined table 2. In my attempts using Sequelize, I used 'include' to quer ...

Error: The variable jQuery.atmosphere is not defined in the packed.js file on line 226

Has anyone encountered this specific error before? Error encountered: TypeError: jQuery.atmosphere is undefined packed.js:2261 I have searched for documentation regarding this issue but could not find any. I have ensured that all required dependencies ar ...

Unable to Access Camera in Angular5 Instascan: <video> Issue

Having Trouble Displaying Camera View with Angular5 and Instascan, Despite Camera Being On app.component.ts constructor(){ this.Instascan= require('instascan'); let scanner = new this.Instascan.Scanner({ video: document.getElementById("pr ...

Guide on implementing route segments using $location.path() in Angular

I am currently utilizing the routesegment provider for routing within Angular. I have a question regarding how to initiate a URL call using $location.path(). Here is my code snippet for reference: FinancialGameApp.config(['$routeSegmentProvider& ...

Encountering an error when trying to inject the dependency for angularMoment

I've been attempting to integrate the moment.js API by adding angularMoment as a dependency to my module, but I'm encountering this error: Error: [$injector:unpr] http://errors.angularjs.org/1.6.4/$injector/unpr?p0=momentProvider%20%3C-%20moment ...

Changing the request body and then routing it through Node.js

I am new to Node.js. I have been working for two days on modifying the body of a Request in Node.js and forwarding it using the http-proxy module for proxying. My goal is to intercept a user's password within a JSON object, encrypt it, and then updat ...

Unable to get the jQuery click event handler to function properly

I am currently working on an asp.net web application in VS 2013. The application utilizes nested master pages, with the main master page containing the following code snippet: <!DOCTYPE html> <script src="http://code.jquery.com/jquery-lat ...

The spotlight is shifting towards validating a datepicker field using jQuery

I am currently working on validating a datepicker field using jQuery. However, whenever I try to validate it, the calendar popup opens up. I only want to display the validation message without the datepicker popup. ...