获取我的 API 后,在 react 中将objectID
转换为字符串时遇到问题...... 例如"ObjectId(5e23828631c3f20fd40d3feb(",我无法得到这个。 我只需要这个"5e23828631c3f20fd40d3feb" 这是我的代码:
import React, { Component } from 'react'
import jwt_decode from 'jwt-decode'
import ObjectID from 'bson-objectid'
class Profile extends Component {
constructor() {
super()
this.state = {
full_name: '',
username: '',
email: '',
pubCount: '',
abmnCount: '',
abneCount: '',
errors: {}
}
this.loadProfile = this.loadProfile.bind(this)
}
loadProfile(){
const token = localStorage.usertoken
fetch('http://localhost:9000/kotgram/user/mesinfos', {
method: 'get',
headers: new Headers({
'Authorization': token,
'Content-Type': 'application/json'
})
}).then(res => res.json())
.then(data => {
console.log(data._id.toString()) /*this shows [object][object] when I try to convert it to a string. I tried to import bson-objectid but it doesnt work*/
console.log(data.email)
this.setState({
full_name: data.full_name,
username: data.username,
email: data.email,
pubCount: data.pubCount,
abmnCount: data.abmnCount,
abneCount: data.abneCount
})
})
.catch(error => console.log('ERROR'));
}
componentDidMount() {
this.loadProfile()
}
render() {
return (
<div className="container">
<div className="jumbotron mt-5">
<div className="col-sm-8 mx-auto">
<h1 className="text-center">PROFILE</h1>
</div>
<table className="table col-md-6 mx-auto">
<tbody>
<tr>
<td>Nom complet</td>
<td>{this.state.full_name}</td>
</tr>
<tr>
<td>Username</td>
<td>{this.state.username}</td>
</tr>
<tr>
<td>Email</td>
<td>{this.state.email}</td>
</tr>
<tr>
<td>Publications</td>
<td>{this.state.pubCount}</td>
</tr>
<tr>
<td>Abe</td>
<td>{this.state.abneCount}</td>
</tr>
<tr>
<td>Abn</td>
<td>{this.state.abmnCount}</td>
</tr>
</tbody>
</table>
</div>
</div>
)
}
}
export default Profile
我已经使用Kotlin作为我的后端服务器和MongoDB来存储我的集合。 请帮忙..
我刚刚找到了一个解决方案: 我回到我的后端服务器并替换了它@Id val _id: ObjectId?
有了这个@Id val _id: String = ObjectId().toHexString()
这样我就只有 objectId 的值作为"hexString",而不是像"machineIdentifier"或"processIdentifier"这样的其他参数