无法使用 mongoose 将数组数据插入 mongodb
Unable to insert array data into mongodb using mongoose
- 我正在尝试将数据插入到我的 mongodb 数据库中。我正在使用 mern 堆栈。
- 这是我的城市数据用户模型,数组中包含
city_name
& city_code
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}],
- 控制台中未返回任何错误消息。我正在使用一条消息,如果用户注册成功,它 returns
User Registered
否则就是错误消息。但是我在控制台中没有得到任何东西。
- 在前端定义的构造函数获取值
city: [{city_name: '', city_code: ''}],
- 已更新
- 这是我用来 post 数据的函数
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>
- 我正在尝试将数据插入到我的 mongodb 数据库中。我正在使用 mern 堆栈。
- 这是我的城市数据用户模型,数组中包含
city_name
&city_code
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}],
- 控制台中未返回任何错误消息。我正在使用一条消息,如果用户注册成功,它 returns
User Registered
否则就是错误消息。但是我在控制台中没有得到任何东西。 - 在前端定义的构造函数获取值
city: [{city_name: '', city_code: ''}],
- 已更新
- 这是我用来 post 数据的函数
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>