0
这是我的node.js代码,当我发送http请求时,从我的应用程序获得重复的json数据,每次请求。这意味着如果发出新的请求,则前一个会话不会过期,它将两次发送相同的json数据,三次等等。在节点js代码中获得重复的json数据
app.get('/ecom/products', function (req, res) {
mysql.Connection.query('select * from product', function (err, result) {
if (err) {
throw err;
res.end("error!!!");
}
else if (result.length > 0) {
for (var i = 0; i < result.length; i++) {
objs.push({
name: result[i].name,
description: result[i].description,
category: result[i].category,
price: result[i].price,
quantity: result[i].quantity,
shipping: result[i].shipping,
location: result[i].location,
color: result[i].color,
link: result[i].link
});
}
res.status(200).send(JSON.stringify(objs));
}
else {
console.log('error occured');
res.end("error occured!");
}
})
});
'objs'声明在哪里? – skellertor
它是全球性的。在模块声明后的顶部 –
如果'objs'数组是全局声明的,那么它会被创建一次,并且不断将数据压入其中。你需要在路由的范围内声明'objs'。这种方式在路由被调用时被创建。它会是空的,直到你推入下一个对象,并总是返回一个单一的东西,而不是积累的东西。 – skellertor