0
  • I am trying to insert data into my mongodb database. I am using mern stack.
  • This is my user model for the city data that is in array containing city_name & city_code
city: [{
    city_name: {type: String},
    city_code: {type: String},
  }],
  • I am passing the values to the register function in this manner
city: [{city_name: this.state.c_name, city_code: this.state.c_code}],
  • This is the function defined to register into the database
city: [{ city_name: req.body.c_name, city_code: req.body.c_code}],
  • No error's message is being returned in the console. I am using a message where if the user is registered successfully it returns User Registered or else the error message. But I am not getting anything in the console.
  • Constructor defined in the front end side to get the values
city: [{city_name: '', city_code: ''}],
  • UPDATED
  • This is the function I am using to post the data
export const register = newUser => {
  return axios
    .post('users/sign-up', {
      username: newUser.username,
      email: newUser.email,
      phone: newUser.phone,
      dob: newUser.dob,
      city: [{city_name: newUser.c_name, city_code: newUser.c_code}],
      password: newUser.password
    })
    .then(response => {
      console.log('Registered')
    })
}
Lav Sharma
  • 327
  • 1
  • 5
  • 23
  • You want to push the data into the array. Can you share more of the code you are using to add this data via mongoose? Here is an example of pushing data into an array: https://stackoverflow.com/questions/33049707/push-items-into-mongo-array-via-mongoose – jacobedawson Jul 17 '20 at 16:27
  • I have updated the code. Check that and let me know or else I will update with some more information if needed. – Lav Sharma Jul 17 '20 at 16:34
  • It has been solved now. – Lav Sharma Jul 17 '20 at 17:39
  • You should update your code with the solution so that any other people searching for this problem will see how you solved it :) – jacobedawson Jul 19 '20 at 09:53

1 Answers1

0
  • SOLUTION
  • So whenever you are accessing the data stored in array we need to give the index value.
<td>{this.props.obj.city[0].cityname}</td>
<td>{this.props.obj.city[0].citycode}</td>
Lav Sharma
  • 327
  • 1
  • 5
  • 23