简体   繁体   中英

How to update user inputted data into JSON

Using javascript, how would I go about creating a function that allows users to edit/update their user profile page (html) that is rendered from the Json file? The user first inputs their data into a page that requests the user response (js file in a routes folder) and then the Json file is rendered under profile html page through another js route.

    var data = require("../data.json");

    exports.addUser = function(request, response) {      
        console.log(data);
        var name = request.query.name;
        var gender = request.query.gender;
        var height = request.query.height;
        var weight = request.query.weight;
        var newUser = {"name": name, "gender": gender, "height": height, "weight": weight};
        data.users.push(newUser);
        response.render('home');
    }

    // Get all of our user data
    var data = require('../data.json');

    exports.view = function(request, response){
        console.log(data);
        response.render('profile', data);
    };

My question is how would I create a function that allow users to make changes to their profile if they have already saved their information once? My app is about the gym with progress as one of the main focus. The user's first inputted information such as weight will not be the same as they progress through the gym, so I want to enable users to edit that information as they progress.

normally you would have an "edit profile" page that retrieves the info from the server/database and then re-saves when you press the "save" button.

If you want this profile info available on other pages, you could also save your user profile json to localStorage.

You would probably want to add an edit user page and find the existing user from the data.users array then edit their information. You could use something similar to the function below.

exports.editUser = function(request, response) {
    var name = request.query.name;
    var gender = request.query.gender;
    var height = request.query.height;
    var weight = request.query.weight;


    editDetails(data.users[findUser(name, data.users)]);
    response.render('home');

    function editDetails(array_index){
        data.users[array_index].name = name;
        data.users[array_index].gender = gender;
        data.users[array_index].height = height;
        data.users[array_index].weight = weight;
    }

    function findUser(name, array){
        for (let i = 0; i < array.length; i++) {
            if (array[i].name === name) {
                return i;
            }
        }
    }
}

The problem with this function would be if two names are the same, then it would edit the first instance of the name. You could add an unique id to make sure that the entry edited is exactly what was intended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM