【发布时间】: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