无法在JavaScript中将ObjectId转换为String

问题描述 投票:0回答:1

嗨,在获取我的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来存储我的收藏集。请帮助..

reactjs mongodb api fetch objectid
1个回答
0
投票

我刚刚找到了解决方案:我回到后端服务器,替换了这个@Id val _id: ObjectId?有了这个@Id val _id: String = ObjectId().toHexString()这样,我将只有objectId的值作为“ hexString”,而不是其他参数,例如“ machineIdentifier”或“ processIdentifier”]

© www.soinside.com 2019 - 2024. All rights reserved.