Reactjs 如何在react中的post请求后获取添加项的id?

Reactjs 如何在react中的post请求后获取添加项的id?,reactjs,mongoose,crud,Reactjs,Mongoose,Crud,我有一个函数,它接受两个输入并在react中发出post请求: addNote(title, content){ this.setState({title: title, content: content, error: ''}, () => { const newNote = { title: this.state.title, content: this.state.content, cre

我有一个函数,它接受两个输入并在react中发出post请求:

addNote(title, content){
    this.setState({title: title, content: content, error: ''}, () => {
        const newNote = {
            title: this.state.title,
            content: this.state.content,
            createdAt: new Date().toISOString(),
            updatedAt: new Date().toISOString()
        };
        fetch('http://localhost:8000/notes', {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(newNote)
        })
        .catch(error => {
            this.setState({error: 'Error adding note.'});
            console.error('Error during adding note', error);
        })
        const storedNotes = this.state.notes;
        storedNotes.unshift(newNote);
        this.setState({notes: storedNotes});
        //Adding this.fetchNotes(); here does not work
    });
}

fetchNotes(){
    fetch('http://localhost:8000/notes')
    .then(response => response.json())
    .then(data => this.setState({isLoading: false, notes: data}))
}
纸币的id类似于猫鼬提供的5B0FAC24637E1005CA0A7B5。如何在react中获取新添加项目的id,以便执行其他操作,如更新和删除route/notes/:noteId中的注释?当我添加注释并尝试删除它时,由于id未定义,因此出现404错误。在页面重新加载时,操作工作正常。我不想在服务器中发出post请求,因为视图是由react呈现的

添加注释后,我再次尝试获取所有注释的请求。它不能按预期工作。添加的注释不会在提交时立即显示


我是否应该用自己的id(比如new Date.toISOString)发出post请求?

为什么不在post请求后返回新的mongoose id

大概是这样的:

addNote(title, content){
    this.setState({title: title, content: content, error: ''}, () => {
        const newNote = {
            title: this.state.title,
            content: this.state.content,
            createdAt: new Date().toISOString(),
            updatedAt: new Date().toISOString()
        };

        fetch('http://localhost:8000/notes', {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(newNote)
        })
        .then(response => response.json())
        .then(data => {
            // here data.id might contain your objectId

            const storedNotes = this.state.notes;
            storedNotes.unshift(newNote);
            this.setState({notes: storedNotes});
            //Adding this.fetchNotes(); here does not work
        })
        .catch(error => {
            this.setState({error: 'Error adding note.'});
            console.error('Error during adding note', error);
        });
    });
}

fetchNotes(){
    fetch('http://localhost:8000/notes')
    .then(response => response.json())
    .then(data => this.setState({isLoading: false, notes: data}))
}

但这需要对服务器上的/notes端点进行一些重构。

除非您显示实际添加到数据库的服务器端代码,否则谁知道该用什么来回答?前端实际上与此无关,因为它所做的只是要求后端做一些事情。所以你给我们看的代码是错误的。@Neil Lunn,非常感谢你。我在服务器中添加了line data=>res.senddata,并且能够获得id。非常感谢。我现在可以拿到身份证了!