我正在尝试从Node https服务器返回一个数组数组。我在客户机上得到的只是第一个数组元素。这是客户端代码:
handleItemList() {
document.getElementById(`itemListBtn`).addEventListener(`click`, () => {
this.setDivDisplay(`itemListDiv`);
fetch(document.url, {
method: 'POST',
headers: {
'x-requested-with': 'fetch.1',
'mode': 'no-cors'
}
}).then(function (response) {
return response.text();
}).then(function (data) {
console.log(data); //outputs 1st MD array element only!
}).catch(function (error) {
console.log(error);
});
});
}
这是服务器代码:
if (request.headers['x-requested-with'] === 'fetch.1') {
this.data_handler.getAllItems(function (fetchedData) {
console.log(fetchedData); //outputs entire MD array! (as string)
response.writeHead(200, {'content-type': 'text/plain'});
response.end(fetchedData);
});
}
这是DataHandler类方法:
getAllItems(callback) {
this.db.all(`SELECT * FROM psp_assets`, function(err, rows) {
rows.forEach(function (row) {
let data = [row.maker, row.model, row.tag, row.sn, row.type, row.description, row.warranty, row.purchaseDate, row.isTitle1, row.isTitle9, row.is31a];
callback(JSON.stringify(data));
});
});
}
以下是MD数组中的一些示例数据:
["Samsung","srfg Quasi Turbo Monitor",223456,"223344","lcd_monitor","someting",24,"2016-01-20",1,0,0]
["LG","AQ 2013",226122,"345678","lcd_monitor","Monitor",36,"2015-01-03",0,1,0]
["Asus","FG3098",87654,"123987","lcd_monitor","Monitor",12,"2016-04-02",0,0,1]
["Apple","iPad Pro",7654321,"654321","ipad","iPad",12,"2016-01-20",1,0,0]
["Logitech","Cam223",678345,"3456789","consumable","WebCam",6,"2016-01-23",1,0,0]
["HP","Stream Pro G4",76534,"87654321","laptop","Mini lapto",12,"2016-01-20",1,0,0]
["hp","Stream 11 G4 Pro EE",878787,"89743211","laptop","laptop",24,"2017-03-03",0,1,0]
答案 0 :(得分:0)
我知道了!这是因为我在dataHandler()
方法中拥有的代码。这是将数据推入数组的新方法。
getAllItems(callback) {
this.db.all(`SELECT * FROM psp_assets`, function(err, rows) {
let data = [];
rows.forEach(function (row) {
data.push([row.maker,row.model,row.tag,row.sn,row.type,row.description,row.warranty,row.purchaseDate,row.isTitle1,row.isTitle9,row.is31a]);
});
callback(data);
});
}