我在AWS上托管了一个无服务器节点。我正在从客户端发出一个请求,向postgresdb插入一个对象数组。该请求有效载荷具有>10000个对象。
我犯了这个错误CORS策略阻止了从源域在"endpoint/ModelSProductAttributesUploadExcelData"处提取的访问:请求的资源上不存在"Access Control Allow origin"标头。如果不透明响应满足您的需求,请将请求的模式设置为"无cors",以在禁用cors的情况下获取资源。
基本上,服务器在发送请求之前超时。我能做些什么来防止这种情况发生?
我的插入逻辑如下:
if (
data[0].hasOwnProperty("SFC") &&
data[0].hasOwnProperty("StyleDesc") &&
data[0].hasOwnProperty("FabricDesc") &&
data[0].hasOwnProperty("ColorDesc") &&
data[0].hasOwnProperty("EventDesc") &&
data[0].hasOwnProperty("DptDesc") &&
data[0].hasOwnProperty("SubDptDesc") &&
data[0].hasOwnProperty("ClassDesc") &&
data[0].hasOwnProperty("InCollection") &&
data[0].hasOwnProperty("Function")
) {
const altered = data.reduce(
(acc, item) =>
(({ SFC, ...rest }) =>
Object.entries(rest).reduce(
(result, [Attribute, Value]) => [
...result,
{ SFC, Attribute, Value, Username: username },
],
acc
))(item),
[]
);
let final_array = altered.filter(
(item) =>
item.Attribute !== "StyleDesc" &&
item.Attribute !== "FabricDesc" &&
item.Attribute !== "ColorDesc" &&
item.Attribute !== "EventDesc" &&
item.Attribute !== "DptDesc" &&
item.Attribute !== "SubDptDesc" &&
item.Attribute !== "SubDptDesc" &&
item.Attribute !== "ClassDesc" &&
item.Attribute !== "InCollection" &&
item.Attribute !== "Function"
);
let sfcs = _.uniqBy(final_array, "SFC").map((item) => item.SFC);
sfcs = sfcs.map((sfc) => `'${sfc}'`);
console.log(sfcs);
try {
const client = connection();
await client.connect();
let query1 = `DELETE from models."InputAttributes" where "SFC" in (${sfcs})`;
const result1 = await client.query(query1);
console.log(result1);
query2 = `INSERT into models."InputAttributes" ("SFC", "Attribute", "Value","Username","CreatedDate") VALUES`;
for (let i = 0; i < final_array.length; i++) {
if (i === final_array.length - 1) {
string = `('${final_array[i].SFC}','${final_array[i].Attribute}','${final_array[i].Value}','${final_array[i].Username}',CURRENT_TIMESTAMP)`;
} else {
string = `('${final_array[i].SFC}','${final_array[i].Attribute}','${final_array[i].Value}','${final_array[i].Username}',CURRENT_TIMESTAMP),`;
}
query2 += string;
}
const result2 = await client.query(query2);
if (result2) {
res.sendStatus(200);
}
} catch (error) {
res.sendStatus(500);
}
} else {
res.sendStatus(400);
}
我认为错误不是因为请求超时,而是因为您没有在服务器中启用CORS策略。因此出现错误:被CORS策略阻止。
//enable cors
var cors = require('cors')
var app = express()
app.use(cors())
关于问题的第二部分,发送请求而不等待,只发送响应而不进行处理,如果这对您的情况有效的话。
//It would look something like this.
async (req, res)=>{
res.send(responseData)
// do the remaining processing here
}
if (
data[0].hasOwnProperty("SFC") &&
data[0].hasOwnProperty("StyleDesc") &&
data[0].hasOwnProperty("FabricDesc") &&
data[0].hasOwnProperty("ColorDesc") &&
data[0].hasOwnProperty("EventDesc") &&
data[0].hasOwnProperty("DptDesc") &&
data[0].hasOwnProperty("SubDptDesc") &&
data[0].hasOwnProperty("ClassDesc") &&
data[0].hasOwnProperty("InCollection") &&
data[0].hasOwnProperty("Function")
) {
const altered = data.reduce(
(acc, item) =>
(({ SFC, ...rest }) =>
Object.entries(rest).reduce(
(result, [Attribute, Value]) => [
...result,
{ SFC, Attribute, Value, Username: username },
],
acc
))(item),
[]
);
let final_array = altered.filter(
(item) =>
item.Attribute !== "StyleDesc" &&
item.Attribute !== "FabricDesc" &&
item.Attribute !== "ColorDesc" &&
item.Attribute !== "EventDesc" &&
item.Attribute !== "DptDesc" &&
item.Attribute !== "SubDptDesc" &&
item.Attribute !== "SubDptDesc" &&
item.Attribute !== "ClassDesc" &&
item.Attribute !== "InCollection" &&
item.Attribute !== "Function"
);
let sfcs = _.uniqBy(final_array, "SFC").map((item) => item.SFC);
sfcs = sfcs.map((sfc) => `'${sfc}'`);
console.log(sfcs);
try {
const client = connection();
await client.connect();
let query1 = `DELETE from models."InputAttributes" where "SFC" in (${sfcs})`;
const result1 = await client.query(query1);
console.log(result1);
let query2 = `INSERT into models."InputAttributes" ("SFC", "Attribute", "Value","Username","CreatedDate") VALUES`;
for (let i = 0; i < final_array.length; i++) {
if (i === final_array.length - 1) {
string = `('${final_array[i].SFC}','${final_array[i].Attribute}','${final_array[i].Value}','${final_array[i].Username}',CURRENT_TIMESTAMP)`;
} else {
string = `('${final_array[i].SFC}','${final_array[i].Attribute}','${final_array[i].Value}','${final_array[i].Username}',CURRENT_TIMESTAMP),`;
}
query2 += string;
}
const result2 = await client.query(query2);
// if (result2) {
res.send(result2);
// }
} catch (error) {
res.sendStatus(500);
}
} else {
res.sendStatus(400);
}
};