0

If I have some data in my Vue I want to parse to my express server, how can this be done? E.g. in the example below, I want to parse what I currently console log in my vue function, to the variable "id" on my server side

expressApp.post('/delete' , function (request, response) {

    const id = request.body.id;
    console.log(id)

        MongoClient.connect(url, function (err, db) {

            if (err) throw err;
            let dbo = db.db(dbName);

            dbo.collection("Members").deleteOne({"_id": objectId(id)}, function (err, res) {
                if (err) throw err;
                db.close();
            });
        });
    response.redirect('/agileApp');
    });


t: function (index) {
            fetch(membersUrl).then(function(response) {
                return response.json();
            }).then(function (data) {
                const formData = new FormData();
                formData.append("id", data[index]._id);
                for (var pair of formData.entries()) {
                    console.log(pair[0]+ ', ' + pair[1]);
                }

                fetch(deleteUrl, {
                    method: 'POST',
                    body: formData
                })
            })
        }

1 Answer 1

1

expressApp.post('/delete' , function (request, response) {

    const id = request.body.id; // <--- get id from the request body

        MongoClient.connect(url, function (err, db) {

            if (err) throw err;
            let dbo = db.db(dbName);

            dbo.collection("Members").deleteOne({"_id": objectId(id)}, function (err, res) {
                if (err) throw err;
                db.close();
            });
        });
    response.redirect('/agileApp');
    });


t: function (index) {
            fetch(membersUrl).then(function(response) {
                return response.json();
            }).then(function (data) {
                const formData = new FormData();
                formData.append("id", data[index]._id);

                fetch('/delete', {
                  method: 'POST', // <---fetch POST method
                  body: formData
                })
            })
        }

Sign up to request clarification or add additional context in comments.

18 Comments

Thank you. It says "const id" is undefined though.
it maybe another issue in your express app.
did you set body-parser ?
Yes I should have npm installed body-parser. Is this the correct way to call it: "<form action="/delete" method="post"> <button type="submit" class="buttons btn btn-outline-danger" @click="ts(index)"><i class="fa">&#xf014;</i> Delete</button> </form>
yes, to check if your backend express app is working properly, try to request /delete api using postman.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.