所以我有一个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
谢谢大家,如果我缺少任何信息,请告诉我
答案 0 :(得分:1)
在您的routes.js
中,您没有获得通过POST方法从NewUser
传入的对象main.js
。您可以使用req.body
对象来获取它。
此外,为避免警告,您应将声明的对象传递给connect
函数,如下所示:
mongoose.connect('mongodb://user:password@sample.com:port/dbname', { useNewUrlParser: true })
答案 1 :(得分:0)
这是一个非常愚蠢的错误,在routes.js
上,将bsonType
更改为type
,将"string"
更改为string