gpt4 book ai didi

javascript - node.js 错误 - 发送后无法发送 header

转载 作者:行者123 更新时间:2023-11-30 08:43:41 26 4
gpt4 key购买 nike

我正在为 node.js 使用 ebay-api,当我出于某种原因刷新页面时,我在控制台中收到错误消息:

can't send headers after they are sent

这是我的代码,谁能帮我理解为什么刷新页面时会出现错误?

// example simple request to FindingService:findItemsByKeywords

var ebay = require('../index.js');
var http = require('http');

var express = require('express');
var app = express();
var io = require('socket.io');


app.set('port', process.env.PORT || 5000);

app.get('/getEbay', function (req, res) {
console.log('inside get');
// for avoiding crossbrowser-error
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.header('Content-Type', 'application/json');
var params = {};

params.keywords = ["cat"];
params['paginationInput.entriesPerPage'] = 10;
ebay.ebayApiGetRequest({
serviceName: 'FindingService',
opType: 'findItemsByKeywords',
appId: 'MYAPPID', // FILL IN YOUR OWN APP KEY, GET ONE HERE: https://publisher.ebaypartnernetwork.com/PublisherToolsAPI
params: params,
// filters: filters,
parser: ebay.parseItemsFromResponse // (default)
},
// gets all the items together in a merged array
function ebayApiGetRequest(error, items) {
if (error) throw error;

console.log('Found', items.length, 'items');
// res.send(items);
console.log(JSON.stringify(items));


res.contentType('application/json');

res.send(JSON.stringify(items));

// }
}
);

});


http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});

console.log('Listening on port 5000...');

我认为我的错误可能在于调用函数的顺序,或者也许有一种方法可以避免这种错误调用?

最佳答案

代替这个:

res.send(JSON.stringify(items));
res.send(items);

尝试:

res.end(JSON.stringify(items));

关于javascript - node.js 错误 - 发送后无法发送 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23911549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com