通過post提交數(shù)據(jù),然后node解析后,使用MongoDB插入到集合。
前端要怎么編碼這些數(shù)據(jù),然后再通過nodejs正確的解析出來?
使用的是axios進(jìn)行post請(qǐng)求的,一開始在前端使用JSON.stringify()但是不行,nodejs解析出來是這樣的:
data = [{'goodsId':1,'goodsName':'雞米花','price':10}] ==> {'{'goodsId':1,'goodsName':'雞米花','price':10}': ''}
nodejs解析后,把我的數(shù)據(jù)直接變成屬性了……。
還試過直接post數(shù)據(jù)過去后端,但后端拿到的是[obejct object]
通過搜索后,我試了QS模塊,使用了Qs.stringify(data),后端終于拿到數(shù)據(jù)了,但是只能拿一條。如果我試圖在data里面添加多一個(gè)對(duì)象,它無法正確的解析2條數(shù)據(jù),會(huì)把同樣屬性的疊加進(jìn)去。我還是貼圖和貼代碼吧!
nodejs代碼
const express = require('express');
const app = express();
const fs = require('fs');
const bodyParser = require("body-parser");
const MongoClient = require("mongodb").MongoClient;
const url = "mongodb://localhost:27017/";
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
//設(shè)置跨域訪問
app.all('*',function(req,res,next) {
res.header('Access-Control-Allow-Origin', '*');
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By", ' 3.2.1');
res.header("Content-Type", "application/json;charset=utf-8");
next();
})
//食物套餐請(qǐng)求
app.get('/foodmeal',function(req,res) {
//讀取文件
fs.readFile('foodmeal.json',function(err,data){
if(err) return console.error(err);
//響應(yīng)http請(qǐng)求
res.send(data);
});
});
//食物詳情請(qǐng)求
app.get('/foods',function(req,res) {
//讀取文件
fs.readFile('foods.json',function(err,data) {
if(err) return console.error(err);
res.send(data);
});
});
app.post("/settle_post", function (req, res) {
//在這里面來操作MongoDB數(shù)據(jù)庫
MongoClient.connect(url,function(err,db) {
if(err) throw err;
//鏈接數(shù)據(jù)庫
let dbo = db.db('foodmeal');
//要插入的數(shù)據(jù)
console.log(req.body);
let myobj = [req.body];
// console.log(myobj);
//鏈接集合,并且插入數(shù)據(jù)
dbo.collection('food').insertMany(myobj,function(err,res){
if(err) throw err;
console.log('插入了' + res.insertedCount);
db.close();
});
})
// console.log(req.body);
res.send();
});
app.listen(3000);
console.log('listening on port 3000...');
{ goodsId: '1',
goodsName: [ '香辣雞腿堡', '田園雞腿堡' ],
price: [ '18', '15' ],
count: [ '1,goodsId=2', '1' ] }
//這里是添加了兩條數(shù)據(jù)后顯示的解析出來的結(jié)果,只有g(shù)oodsid沒被疊加,但不管有沒疊加,我希望的是兩條數(shù)據(jù),然后可以使用MongoDB進(jìn)行插入
前端代碼
let data = [];
//this.tableData ==>需要提交的數(shù)據(jù)
this.tableData.forEach((item,i) => {
data[i] = Qs.stringify(item);
});
// let data = Qs.stringify(this.tableData[0]);一條數(shù)據(jù)的話沒問題
console.log(data);
axios({
url:'http://localhost:3000/settle_post',
method:'post',
data:data,
transformRequest: [function(data){
return data;
}],
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
因?yàn)閯倢W(xué)的node沒兩天,有些東西不清楚,百度了各種方法還是未得結(jié)果,只能求助各位大佬啦!因?yàn)樾〉苤R(shí)比較單薄,如果還需要提供什么東西,請(qǐng)?jiān)谠u(píng)論里面提醒。謝謝
ps:(目前想到個(gè)很浪費(fèi)的方法,循環(huán)數(shù)據(jù),然后每條數(shù)據(jù)進(jìn)行一次post請(qǐng)求- -)
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國成功上市,融資1
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。