从javascript获取将文档插入mongoDB数据库

从javascript获取将文档插入mongoDB数据库,javascript,mongodb,Javascript,Mongodb,我正在编写一个应用程序,需要在mongoDB数据库中插入一个文档。这是我在reactJS应用程序中执行提取以插入文档的代码: var myJSON = JSON.stringify(obj); console.log(myJSON); var url = "https://api.mlab.com/api/1/databases/test-db/collections/buildings?apiKey=xxxxxxxxxxN2ZvobziWxyhx

我正在编写一个应用程序,需要在mongoDB数据库中插入一个文档。这是我在reactJS应用程序中执行提取以插入文档的代码:

        var myJSON = JSON.stringify(obj);
        console.log(myJSON);
        var url = "https://api.mlab.com/api/1/databases/test-db/collections/buildings?apiKey=xxxxxxxxxxN2ZvobziWxyhxxxxxxxxxx";

        const options = { 
            method: 'POST', 
            data: myJSON, 
            headers: new Headers({'Content-Type': 'application/json' }) 
        };

        fetch( url, options)
        .then(function(response) {
            return response.json();
        })
        .then(function(myJson) {   
            console.log("returned data: ", myJson);                
        });
当我执行我的应用程序时,我看到console.log(),我看到了发送到mongoDB的字符串,我看到了console.log(),上面写着“returned data:null”

然后,我从发送到mongoDB的console.log中获取字符串,并将其输入到POSTMAN中。我设置POSTMAN以执行POST,并在标题中输入内容类型的键和application/json的值。当我发送请求时,我得到一个响应,然后我查看mongoDB数据库并验证文档是否为ins埃特德

为什么文档不是从javascript fetch()插入的,而是从POSTMAN插入的


谢谢。

使用
fetch
,我相信选项对象中的
数据
字段应该被称为
body
@sketched,因为您100%正确。我将数据更改为body,并插入了文档。非常感谢!