无法使用 mongoose 将数组数据插入 mongodb

Unable to insert array data into mongodb using mongoose

city: [{
    city_name: {type: String},
    city_code: {type: String},
  }],
city: [{city_name: this.state.c_name, city_code: this.state.c_code}],
city: [{ city_name: req.body.c_name, city_code: req.body.c_code}],
city: [{city_name: '', city_code: ''}],
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')
    })
}
  • 解决方案
  • 因此,无论何时您访问存储在数组中的数据,我们都需要提供索引值。
<td>{this.props.obj.city[0].cityname}</td>
<td>{this.props.obj.city[0].citycode}</td>