【问题标题】:Axios.Post is not sending data to mongodb working on node.jsAxios.Post 没有向处理 node.js 的 mongodb 发送数据
【发布时间】:2019-03-18 18:11:18
【问题描述】:

所以我有一个 POST 调用,它似乎正在工作,唯一的问题是没有将数据发送到数据库。请参阅下面的导入代码:提前致谢!

module.js

var mongoose = require ( 'mongoose' );

var CryptoWallet = new mongoose.Schema({
  publicK:{
    bsonType: "String"
  },
  seed:{
    bsonType: "String"
  }
},{
    collection : 'test1a' //
  });

module.exports = mongoose.model('test1', CryptoWallet); //Change from test1

routes.js

var CryptoWallet = require('../models/CryptoWallet-model');

//Defined store route --Need to get the right functions to generate data
router.route('/add/createCryptoWallet').post(function(req, res){

   var crypto_wallet = new CryptoWallet(req.headers)
     console.log("The cyrptoWallet on create", crypto_wallet);
     //console.log(wallet);
      crypto_wallet.save()
      .then(crypto_wallet =>{
        res.status(200).json({status: 'CryptoWallet addded succesfully'});//,CryptoWalletObject: crypto_wallet
      })
      .catch(err => {
        res.status(400).send("unable to save CryptoWallet to databse");
      });
});

main.js

...
var address = "Ox" + addresses;
                    var seedPhrase = seed;
                    html = html + "<p><b>Address:</b>" + address  + "</p>"; //html = html + "<p><b>Address: </b>0x" + address  + "</p>";        
                }
                document.getElementById("address").innerHTML = html;
                    console.log(seedPhrase)
                    console.log(typeof seedPhrase)
                    console.log(address)
                    console.log(typeof seedPhrase)
                    addToAPI(seedPhrase,address); //address
            }
        });
    });
}
        function addToAPI(seedPhrase,address){
            let NewUser = {
         publicK:address,
                 seed: seedPhrase
             }
      console.log(NewUser);
      axios.post('http://localhost:3000/CryptoWallet/add/createCryptoWallet', NewUser)
      .then((response)=>{
        console.log(response);
                console.log(response.data);
      })
      .catch((error)=>{
        console.log(error);
      });
    }

这是来自网络控制台的响应: enter image description here 和我终端上的控制台: enter image description here

谢谢大家,如果我遗漏了任何信息,请告诉我

【问题讨论】:

    标签: node.js mongodb post mongoose axios


    【解决方案1】:

    在您的routes.js 中,您没有从 POST 方法中的main.js 获取对象NewUser。您可以使用req.body 对象来获取它。

    另外,为了避免警告,您应该将声明的对象传递给connect 函数,如下所示:

    mongoose.connect('mongodb://user:password@sample.com:port/dbname', { useNewUrlParser: true })
    

    【讨论】:

    • 您好,感谢您的帮助,不幸的是,它不起作用。看起来我的对象 NewUser 仍然没有通过。另外,感谢您的警告修复,它确实有效。
    • 如果您在routes.js 中添加console.log(req.body),您会得到什么?你应该得到你的 NewUser 对象。所以改变crypto_wallet对象创建:使用req.body作为参数而不是req.headers
    • 我从console.log(req.body) 得到undefined。我在我的routes.js 中将req.headers 更改为req.body
    • 你是否在你的api中安装/配置了body-parser?你需要在定义路由之前做。
    • 我刚刚安装/配置了body-parser。现在来自console.log(req.body) 正在显示我的数据{ publicK: '0xc31a31568068963db7b3f022d4beeeba3a658b73', seed: 'chicken convince ordinary door slot valve attitude viable draft toddler tornado solution' },但仍然没有反映在数据库上。当我执行console.log(crypto_walet) 时,它只会给我由 mongo 创建的 _id。
    【解决方案2】:

    这是一个非常愚蠢的错误,在 routes.js 上,将 bsonType 更改为 type"string" 更改为 string

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-27
      • 2021-02-04
      • 1970-01-01
      • 1970-01-01
      • 2019-08-02
      • 2016-10-25
      • 1970-01-01
      相关资源
      最近更新 更多