javascript 中无法将 ObjectId 转换为字符串

Can't convert ObjectId to String in javascript

您好,在获取我的 API 后,无法将 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 来存储我的 collections.. 请帮助..

我刚刚找到了一个解决方案: 我回到我的后端服务器并替换它 @Id val _id: ObjectId? 有了这个 @Id val _id: String = ObjectId().toHexString() 所以我只会将 objectId 的值作为 "hexString" 而不是像 "machineIdentifier" 或 "processIdentifier"

这样的其他参数