how can I store 4input data to anoptions array? - node.js

this is my react code where I want to create polls using react form but I don't understand what goes wrong with my code...!`Here I have 4 input field with { option1: "", option2: "", option3: "", option4: "" }, but I don't know how to store data just like I store data using POSTMAN...!CAN ANYONE HELP PLEASE help....!PLEASE HEPL I DO NOT UNDERSTAND WHAT TO DO...! WITH MY CODE
HELP PLEASE......
import React, { useState, useEffect } from "react";
import "../styles.css";
import { isAutheticated } from "../auth/helper/index";
import { createaPoll } from "./helper/adminapicall";
const AddPoll = () => {
const { user, token } = isAutheticated();
const [value, setValue] = useState({
question: "",
options: { option1: "", option2: "", option3: "", option4: "" },
error: "",
loading: "false",
getRedirect: false,
formData: "",
});
const { question, options, error, loading, getRedirect, formData } = value;
const handleChange = (event) => {
// setError("");
setValue({ ...value, [event.target.name]: event.target.value });
// console.log(event.target.value);
const newOption = {
...value.options,
[event.target.name]: event.target.value,
};
setValue((prev) => ({ ...prev, options: newOption }));
};
const onSubmit = (event) => {
event.preventDefault();
setValue({ ...value, error: "", loading: true });
// console.log(handel);
createaPoll(user._id, token, { question, options }).then((data) => {
if (data.error) {
setValue({ ...value, error: data.error });
} else {
setValue({
...value,
question: "",
options: { option1: "", option2: "", option3: "", option4: "" },
error: "",
loading: "false",
getRedirect: false,
formData: "",
});
}
});
};
return (
<div className="AddPoll">
<div className="container">
<h1>Add New Poll</h1>
<form>
<textarea
rows="4"
cols="50"
className="form-control mb-2"
placeholder="Question"
name="question"
value={question}
onChange={(event) => handleChange(event)}
autoFocus
></textarea>
<input
type="text"
className="form-control mb-2"
placeholder="Option1"
onChange={(event) => handleChange(event)}
name="option1"
value={options.option1}
/>
<input
type="text"
className="form-control mb-2"
placeholder="Option2"
onChange={(event) => handleChange(event)}
name="option2"
value={options.option2}
/>
<input
type="text"
className="form-control mb-2"
placeholder="Option3"
onChange={(event) => handleChange(event)}
name="option3"
value={options.option3}
/>
<input
type="text"
className="form-control mb-2"
placeholder="Option4"
onChange={(event) => handleChange(event)}
name="option4"
value={options.option4}
/>
<button type="submit" onClick={onSubmit} className="btn Submitbtn">
Submit
</button>
</form>
</div>
</div>
);
};
export default AddPoll;
And when I did with POSTMAN it's work fine! Here is my POSTMAN IMAGE
so I don't understand what to do, with my react form code , can anyone help me please....!

Try to separate question and option values in handleChange function.
const handleChange = event => {
if (event.target.name === 'question') {
setValue({ ...value, [event.target.name]: event.target.value });
} else {
const newOption = {
...value.option,
[event.target.name]: event.target.value
};
setValue({ ...value, option: newOption });
}
};

try this :
const [value, setValue] = useState({
question: "",
option: [ option1: "", option2: "", option3: "", option4: "" ],
error: "",
loading: "false",
getRedirect: false,
formData: "",
});
const onSubmit = (event) => {
event.preventDefault();
setValue({ ...value, error: "", loading: true });
// console.log(handel);
createaPoll(user._id, token, { question, option }).then((data) => {
if (data.error) {
setValue({ ...value, error: data.error });
} else {
setValue({
...value,
question: "",
option: ["hello","hi","hey"],
error: "",
loading: "false",
getRedirect: false,
formData: "",
});
}
});
};

Related

pagination with ant design not updating the limit

I am trying to make a pagination table using an ant table. but I am not getting the result I want. I am always getting a limited number of data. can't really update the data limit dynamically. what i am doing wrong here? I am hardcoded the limit here how to do it dynamically ?? how can i fetch the data from data base i can show only 20 data on each page. also i have to show the available buttons
backend
export const getJourneyDetails = async (req, res) => {
try {
const page = parseInt(req.query.page || 1);
const perPage = parseInt(req.query.perPage || 100);
const search = req.query.search || "";
let JourneyDetails = await journey_details
.find({
Departure_Station_Name: { $regex: search, $options: "i" },
})
.skip((page - 1) * perPage)
.limit(perPage);
if (!JourneyDetails.length) {
JourneyDetails = await journey_details.find().limit(perPage);
}
const totalpages = Math.ceil(
(await journey_details.countDocuments()) / perPage
);
res.status(200).json({ JourneyDetails, totalpages });
frontend
import { CircularProgress } from "#mui/material";
import { useEffect, useState } from "react";
import type { ColumnsType } from "antd/es/table";
import { Table } from "antd";
import axios from "axios";
interface JourneyDetail {
Departure_time: String;
Return_time: String;
Departure_Station_Id: number;
Departure_Station_Name: String;
Return_Station_Id: number;
Return_Station_Name: String;
Distance: number;
Duration: number;
}
const JourneyData: React.FC = () => {
const [journeyDetails, setJourneyDetails] = useState<JourneyDetail[]>([]);
const [totalPages, setTotalPages] = useState(1);
const [searchQuery, setSearchQuery] = useState("");
const fetchData = async (page: number) => {
const { data } = await axios.get(
`https://helisinkicitybike.onrender.com/home/journey/?page=${page}&perPage=20&search=${searchQuery}
`
);
setJourneyDetails(data.JourneyDetails);
setTotalPages(data.totalPages);
};
useEffect(() => {
fetchData(1);
}, [searchQuery]);
const columns: ColumnsType<JourneyDetail> = [
{
title: "Departure time",
dataIndex: "Departure_time",
width: 100,
fixed: "left",
},
{
title: "Departure Station Name",
dataIndex: "Departure_Station_Name",
width: 100,
fixed: "left",
},
{
title: "Return time",
dataIndex: "Return_time",
width: 100,
fixed: "left",
},
{
title: "Return Station Name ",
dataIndex: "Return_Station_Name",
width: 100,
fixed: "left",
},
{
title: " Distance ",
dataIndex: "Distance",
width: 100,
fixed: "left",
},
{
title: "Duration ",
dataIndex: "Duration",
width: 100,
fixed: "left",
},
];
if (!journeyDetails) return <CircularProgress />;
return (
<div className="container mt-5">
<div className="input-group mb-3">
<span className="input-group-text" id="inputGroup-sizing-default">
Search
</span>
<input
placeholder=" Enter Station Name"
type="text"
className="form-control"
aria-label="Sizing example input"
aria-describedby="inputGroup-sizing-default"
onChange={(e) => setSearchQuery(e.target.value)}
/>
</div>
<Table
columns={columns}
dataSource={journeyDetails}
pagination={{
pageSize: 10,
total: totalPages,
onChange: (page) => {
fetchData(page);
},
}}
/>
<p style={{ fontSize: "10px", marginTop: "5px" }}>
#Data source Helsinki City Bike, covers the period of May to July 2021.
</p>
</div>
);
};
export default JourneyData;
} catch (error) {
console.error(error);
res.status(500).json({ message: "Error retrieving Journey Details" });
}
};

Updating products with fetch and input value fields

I have spent whole day trying to figure this out. I want to update course recipes from my database (MongoDB) using my REST API call (Node.js with Express) by sumbiting input fields with new values of the recipe. I tried to show previous values by using input value="", but as I learned this makes it to be static. I tried to change it into dynamic accordingly to what I found online however none of tutorials I found would show what I am looking for. As you can see in code below I am trying to PUT new data that was previously set using setState(). Sadly I do not know how can I do it like this. Could you tell me if it is even possible and if so where can I learn to do it?
Here is code from React:
import React, { useState } from "react";
import { useHistory } from "react-router-dom";
import Alert from "../elements/Alert";
import Axios from "axios";
export default function UpdateCourse() {
const id = window.location.href.split("?")[1];
const [dishName, setdishName] = useState("");
const [category, setcategory] = useState("");
const [author, setauthor] = useState("");
const [ingredients, setingredients] = useState([]);
const [cookingTime, setcookingTime] = useState("");
const [sourceUrl, setsourceUrl] = useState("");
const [imageUrl, setimageUrl] = useState("");
const [isPublished, setisPublished] = useState("true");
const [price, setprice] = useState("");
const [tags, settags] = useState([]);
const [alert, setAlert] = useState("");
const history = useHistory();
const url = `http://localhost:1234/api/courses/find/${id}`;
const old = async () => {
const result = await Axios.get(url);
setdishName(result.data.dishName);
setcategory(result.data.category);
setauthor(result.data.author);
setingredients(result.data.ingredients);
setcookingTime(result.data.cookingTime);
setsourceUrl(result.data.sourceUrl);
setimageUrl(result.data.imageUrl);
setisPublished(result.data.isPublished);
setprice(result.data.price);
settags(result.data.tags);
};
old();
console.log(old);
async function update() {
let item = {
dishName,
category,
author,
ingredients,
cookingTime,
sourceUrl,
imageUrl,
isPublished,
price,
tags,
};
console.log(item);
console.log(JSON.stringify(item));
const result = await fetch(`http://localhost:1234/api/courses/${id}`, {
method: "PUT",
body: JSON.stringify(item),
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
});
console.log(result);
if (result.status !== 200) {
return setAlert(result.status + " " + result.statusText);
}
history.push("/");
window.location.reload();
}
return (
<div className="col-sm-6" style={{ textAlign: "center" }}>
<h1 className="bigBar">Update recipe</h1>
<div style={{ marginLeft: "3.5rem" }}>
{alert !== "" && <Alert alert={alert}></Alert>}
</div>
<input
autoFocus="autofocus"
required="required"
type="text"
className="form-control"
placeholder={dishName}
value={dishName}
onChange={e => setdishName(e.target.value)}
/>
<br />
<input
required="required"
type="text"
className="form-control"
placeholder={category}
value={category}
onChange={e => setcategory(e.target.value)}
/>
<br />
<input
type="text"
onChange={e => setauthor(e.target.value)}
className="form-control"
required="required"
placeholder={author}
value={author}
/>
<br />
<input
type="text"
onChange={e => setingredients(e.target.value)}
className="form-control"
required="required"
placeholder={ingredients}
value={ingredients}
/>
<br />
<input
type="text"
onChange={e => setcookingTime(e.target.value)}
className="form-control"
required="required"
placeholder={cookingTime}
value={cookingTime}
/>
<br />
<input
type="text"
onChange={e => setsourceUrl(e.target.value)}
className="form-control"
required="required"
placeholder={sourceUrl}
value={sourceUrl}
/>
<br />
<input
type="text"
onChange={e => setimageUrl(e.target.value)}
className="form-control"
required="required"
placeholder={imageUrl}
value={imageUrl}
/>
<br />
<input
type="text"
onChange={e => setisPublished(e.target.value)}
className="form-control"
required="required"
placeholder={isPublished}
value={isPublished}
/>
<br />
<input
type="text"
onChange={e => setprice(e.target.value)}
className="form-control"
required="required"
placeholder={price}
value={price}
/>
<br />
<input
type="text"
onChange={e => settags(e.target.value)}
className="form-control"
required="required"
placeholder={tags}
value={tags}
/>
<br />
<button onClick={update} className="btn btn-primary">
Submit
</button>
</div>
);
}
Here is put from REST API:
router.put("/:id", async (req, res) => {
const { error } = validateCourse(req.body);
if (error)
//400 Bad request
return res.status(400).send(error.details[0].message);
const course = await Course.findByIdAndUpdate(
req.params.id,
_.pick(req.body, [
`dishName`,
`category`,
`password`,
`ingredients`,
`cookingTime`,
`sourceUrl`,
`imageUrl`,
`isPublished`,
`price`,
`tags`,
]),
{
useFindAndModify: false,
new: true,
}
);
if (!course)
return res.status(404).send(`The course with the given ID was not found`);
res.send(course);
});
And here is sample course from MongoDB:
{
"isPublished": true,
"tags": [
"pizza"
],
"_id": "60ae108ddfb18463c046a5ba",
"dishName": "Pizza with Cauliflower Crust",
"category": "pizza",
"ingredients": [
{
"_id": "60a4cfa48c20aa5c18517606",
"quantity": 1,
"unit": "",
"description": "medium head cauliflower cut into florets"
},
{
"_id": "60a4cfa48c20aa5c18517607",
"quantity": 1,
"unit": "",
"description": "egg"
},
{
"_id": "60a4cfa48c20aa5c18517608",
"quantity": 0.5,
"unit": "cup",
"description": "mozzarella shredded"
},
{
"_id": "60a4cfa48c20aa5c18517609",
"quantity": 1,
"unit": "tsp",
"description": "oregano or italian seasoning blend"
},
{
"_id": "60a4cfa48c20aa5c1851760a",
"quantity": null,
"unit": "",
"description": "Salt and pepper to taste"
},
{
"_id": "60a4cfa48c20aa5c1851760b",
"quantity": 1,
"unit": "cup",
"description": "chicken cooked and shredded"
},
{
"_id": "60a4cfa48c20aa5c1851760c",
"quantity": 0.5,
"unit": "cup",
"description": "barbecue sauce"
},
{
"_id": "60a4cfa48c20aa5c1851760d",
"quantity": 0.75,
"unit": "cup",
"description": "mozzarella shredded"
},
{
"_id": "60a4cfa48c20aa5c1851760e",
"quantity": null,
"unit": "",
"description": "Red onion to taste thinly sliced"
},
{
"_id": "60a4cfa48c20aa5c1851760f",
"quantity": null,
"unit": "",
"description": "Fresh cilantro to taste"
}
],
"cookingTime": 60,
"sourceUrl": "https://www.closetcooking.com/cauliflower-pizza-crust-with-bbq/",
"imageUrl": "https://www.closetcooking.com/wp-content/uploads/2013/02/BBQ-Chicken-Pizza-with-Cauliflower-Crust-500-4699.jpg",
"price": 29.99,
"date": "2021-05-26T09:10:37.620Z",
"__v": 0
}
When button is clicked and the PUT request is made and the values are updated - you must also tell your component states to mirror the new changes from the backend.
Therefor you must call old() method (which handles the fetching request and set states) after your PUT request. This is makes sure that your component states is sync with the values from the database.
Here is a small modification to your update() method (I marked it with an arrow):
async function update() {
let item = {
dishName,
category,
author,
ingredients,
cookingTime,
sourceUrl,
imageUrl,
isPublished,
price,
tags,
};
console.log(item);
console.log(JSON.stringify(item));
const result = await fetch(`http://localhost:1234/api/courses/${id}`, {
method: "PUT",
body: JSON.stringify(item),
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
});
console.log(result);
if (result.status !== 200) {
return setAlert(result.status + " " + result.statusText);
}
history.push("/");
window.location.reload();
old() <----- THIS
}
UPDATE:
I made a small example in Codesandbox that help you display ingredients and change their values.
I havent used any UI libraries but i strongly recommend something like Material UI to implement the containers and display them in a more proper way -it is all up to you how you want to design it.
Well here is it: https://codesandbox.io/s/cocky-sammet-d8wq5?file=/src/App.js

2checkout Payment Authorization Failed in React

I have been using 2checkout payment gateway. I am using the script file provided by 2checkout. First I was importing it in my index.html file by using <link></link> and it was giving me an CORBS error. So I downloaded the script and place in local file.
Now, it's working as expected. It's providing me with authorization token by 2checkout. Following is my component that I have been using to get token form server.
import React, { useEffect, useState } from 'react';
const Form = (props) => {
const [ card, setCard ] = useState({
sellerId: <my-seller-id>,
publishableKey: <my-publishable-key>,
ccNo: '',
expMonth: '',
expYear: '',
cvv: ''
});
const [ returnToken, setReturnToken ] = useState(null);
useEffect(() => {
window.TCO.loadPubKey('sandbox');
}, []);
const submitted = (e) => {
e.preventDefault();
var payWithCard = (data) => {
console.log(data.response.token.token);
};
var error = (error) => {
console.log(error);
};
try {
window.TCO.requestToken(payWithCard, error, card);
} catch (error) {
setTimeout(() => {
window.TCO.requestToken(payWithCard, error, card);
}, 3000);
}
};
const change = (e) => {
setCard({
...card,
[e.target.name]: e.target.value
});
};
return (
<form id="tcoCCForm" onSubmit={submitted}>
<input id="sellerId" type="hidden" value={card.sellerId} />
<input id="publishableKey" type="hidden" value={card.publishableKey} />
<div>
<label>
<span>Card Number</span>
<input
id="ccNo"
name="ccNo"
type="text"
value={card.ccNo}
autoComplete="off"
required
onChange={(e) => change(e)}
/>
</label>
</div>
<div>
<label>
<span>Expiration Date (MM/YYYY)</span>
<input
type="text"
size="2"
id="expMonth"
name="expMonth"
value={card.expMonth}
required
onChange={(e) => change(e)}
/>
</label>
<span> / </span>
<input
type="text"
size="4"
id="expYear"
name="expYear"
value={card.expYear}
required
onChange={(e) => change(e)}
/>
</div>
<div>
<label>
<span>CVC</span>
<input
id="cvv"
name="cvv"
type="text"
value={card.cvv}
autoComplete="off"
required
onChange={(e) => change(e)}
/>
</label>
</div>
<input type="submit" />
</form>
);
};
export default Form;
so, it's giving me the token to console that I am using in Postman for testing the 2checkout api.
https://www.2checkout.com/checkout/api/1/<seller_id>/rs/authService
I have been using following payload to send the POST request to this api.
{
"sellerId": <seller_id>,
"privateKey": <private_key>,
"merchantOrderId": "123",
"token": "N2Y5MDFmNTItYzcxMS00OGQ5LTk2MmItOGJlMjAzYWQwNDFl",
"currency": "USD",
"demo": true,
"lineItems": [
{"name": "Package A", "price": 10, "quantity": 1, "type": "product", "recurrence": "1 Month", "duration": "Forever"} ],
"billingAddr": {"name": "Wasi Ullah", "addrLine1": " village Bharaj P/O Lakhanwal", "city": "Gujrat", "state": "Pubjab", "zipCode": "50700", "country": "Pakistan", "email": "chwasiullah#gmail.com", "phoneNumber": "+923006242851"}
}
While the response I got everytime is:
{
"validationErrors": null,
"response": null,
"exception": {
"exception": false,
"httpStatus": "400",
"errorMsg": "Payment Authorization Failed: Please verify your information and try again, or try another payment method.",
"errorCode": "607"
}
}
Even I have provided with original card and all information in demo mode. But there's still the same issue.
I got the solution to this problem. I want to share it. May be it will be helpful for you.
If you are testing 2checkout don't forget to check the documentation of test orders:
https://knowledgecenter.2checkout.com/Documentation/09Test_ordering_system/01Test_payment_methods
Moreover, I wasn't adding the name according to this test order in api that's why it was saying me that Card Authorization failed.

Is mapDispatchToProps the way to go?

I am following a tutorial for a React+Redux fullstack and the instructor did something strange that is not working for me.
Specifically these lines, in the submitForm() class:
this.props.dispatch(registerUser(dataToSubmit))
.then(response =>{
Are causing error:
TypeError: this.props.dispatch(...).then is not a function
This is the whole class:
import React, { Component } from 'react';
import FormField from '../utils/Form/formfield';
import { update, generateData, isFormValid } from '../utils/Form/formActions';
import { connect } from 'react-redux';
import { registerUser } from '../../actions/user_actions';
class Register extends Component {
state = {
formError: false,
formSuccess:false,
formdata:{
name: {
element: 'input',
value: '',
config:{
name: 'name_input',
type: 'text',
placeholder: 'Enter your username'
},
validation:{
required: true
},
valid: false,
touched: false,
validationMessage:''
},
email: {
element: 'input',
value: '',
config:{
name: 'email_input',
type: 'email',
placeholder: 'Enter your email'
},
validation:{
required: true,
email: true
},
valid: false,
touched: false,
validationMessage:''
},
password: {
element: 'input',
value: '',
config:{
name: 'password_input',
type: 'password',
placeholder: 'Enter your password'
},
validation:{
required: true
},
valid: false,
touched: false,
validationMessage:''
},
confirmPassword: {
element: 'input',
value: '',
config:{
name: 'confirm_password_input',
type: 'password',
placeholder: 'Confirm your password'
},
validation:{
required: true,
confirm: 'password'
},
valid: false,
touched: false,
validationMessage:''
}
}
}
updateForm = (element) => {
const newFormdata = update(element,this.state.formdata,'register');
this.setState({
formError: false,
formdata: newFormdata
})
}
submitForm= (event) =>{
event.preventDefault();
let dataToSubmit = generateData(this.state.formdata,'register');
let formIsValid = isFormValid(this.state.formdata,'register')
if(formIsValid){
this.props.dispatch(registerUser(dataToSubmit))
.then(response =>{
if(response.payload.success){
this.setState({
formError: false,
formSuccess: true
});
setTimeout(()=>{
this.props.history.push('/register_login');
},3000)
} else {
this.setState({formError: true})
}
}).catch(e => {
this.setState({formError: true})
})
} else {
this.setState({
formError: true
})
}
}
render() {
return (
<div className="page_wrapper">
<div className="container">
<div className="register_login_container">
<div className="left">
<form onSubmit={(event)=> this.submitForm(event)}>
<h2>Personal information</h2>
<div className="form_block_two">
<div className="block">
<FormField
id={'name'}
formdata={this.state.formdata.name}
change={(element)=> this.updateForm(element)}
/>
</div>
</div>
<div>
<FormField
id={'email'}
formdata={this.state.formdata.email}
change={(element)=> this.updateForm(element)}
/>
</div>
<h2>Verify password</h2>
<div className="form_block_two">
<div className="block">
<FormField
id={'password'}
formdata={this.state.formdata.password}
change={(element)=> this.updateForm(element)}
/>
</div>
<div className="block">
<FormField
id={'confirmPassword'}
formdata={this.state.formdata.confirmPassword}
change={(element)=> this.updateForm(element)}
/>
</div>
</div>
<div>
{ this.state.formError ?
<div className="error_label">
Please check your data
</div>
:null}
<button onClick={(event)=> this.submitForm(event)}>
Create an account
</button>
</div>
</form>
</div>
</div>
</div>
</div>
);
}
}
export default connect()(Register);
So, I tried to add both:
mapDispatchToProps = (dispatch) => {
return {
registerTheUser: (submitData) => {dispatch(registerUser(submitData)) }
}
and
export default connect(mapDispatchToProps)(Register);
then changed:
this.props.dispatch(registerUser(dataToSubmit))
.then(response =>{
to
this.props.registerTheUser(dataToSubmit)
.then(response =>{
However, that also didn't work.
I am at a complete loss as to what it is I need to do. Is mapDispatchToProps() even the strategy I should be taking to fix this?
I can add more code if necessary.
EDIT, action registerUser():
export function registerUser(dataToSubmit){
const request = axios.post(`http://localhost:4444/users/create`,dataToSubmit)
.then(response => response.data);
return {
type: REGISTER_USER,
payload: request
}
}
mapDispatchToProps is the second argument to connect, the first argument is mapStateToProps
To supply just mapDispatchToProps, you must pass the first argument as null like
export default connect(null, mapDispatchToProps)(Register);
then use it like
this.props.registerTheUser(dataToSubmit)
.then(response =>{
Also the first way is correct, however your dispatch action isn't returning a promise and hence .then cannot be executed on it.
Make sure you use redux-thunk middleware and return a promise
const registerUser = (data) => {
return dispatch => {
return API.register('/url', data) // a return statement here for returning promise
}
}

How to add a key value pair on existing object created via Vue Reactivity

I already have an existing form which is dynamically created. However, I have problems with regards to adding a new set of key value pairs to the existing object. I have used the Vue Reactivity using the this.$set() method with success on the FIRST pair only.
Output
{ "traveller_1": { "gender": "c" },
"traveller_2": { "gender": "f" },
"traveller_3": { "gender": "i" }
}
Expected Output
{ "traveller_1": { "firstname": "John", "age": "23", "gender": "m" },
"traveller_2": { "firstname": "Jane", "age": "21", "gender": "f" },
"traveller_3": { "firstname": "Jade", "age": "25", "gender": "f" },
}
Fiddle https://jsfiddle.net/stda7Lwm/
View
<div class="col-md-10" id="app"> {{ travellerDetails }}
<div class="form-row" v-for="i in travellers">
<div class="form-group col-md-6" v-for="(details, index) in bookingRequiredDetails">
<label for="required-details">{{ details }}</label>
<input
type="text"
class="form-control"
#input="prop('traveller_' + i, details, $event)"
placeholder="Required Details"
/>
</div>
</div>
</div>
JS
new Vue({
el: '#app',
mounted () {
},
data () {
return {
test: { 'unit1' : { life: 30}},
travellerDetails: { },
travellers: 3,
bookingRequiredDetails: ['fullname', 'age', 'gender'],
};
},
methods: {
prop: function(obj, prop, event) {
this.$set(this.travellerDetails, obj, { [prop] : event.target.value } );
console.log(this.travellerDetails);
}
},
})
You're overriding all object every time you assign new value. You should change a single prop only
prop: function(obj, prop, event) {
const data = this.travellerDetails[obj] || {}
data[prop] = event.target.value
this.travellerDetails = {
...this.travellerDetails,
[obj]: {...data}
}
}

Resources