我有一个11ty项目(静态网站生成器),我在那里获取属性数据,然后在Promise中使用此数据。所有调用:
1.获取所有属性映像和
1.获取租赁应用程序URL
然后将它们添加到一个标记为allData的数组中。
图像调用工作正常,但租赁API调用不正常。我正在使用Promise. all中的i将发送到的API主体更改为租赁API,这似乎在我的console. logs中工作,但我得到的结果是每次调用都返回相同的租赁链接。
我觉得这可能与我调用fetch/using Promise的顺序有关。这都是因为如果我添加一个hello控制台日志,我的一个带有i标签的数组将在hello之后记录日志,然后是下一个,例如body 1 hello,body 2 hello,等等,然后是所有的tpResponses(url)将登录一行..这似乎不对。我看不到实际的请求通过,因为这是在构建时完成的,所以我有点困惑,但我'根据结果,我猜只发送了一个apibody。
我试过使用. then来调用fetch,但是我认为我配置错了。我需要使用. then来调用fetch,或者使用Promise. all以外的其他方法吗?
代码如下.任何帮助将不胜感激!
require('dotenv').config();
const EleventyFetch = require("@11ty/eleventy-fetch");
const crypto = require('crypto');
async function getPropertyData(){
console.log("getPropertyData...")
//Property vars
var username = process.env.MANOR_USERNAME;
var password = process.env.MANOR_PASSWORD;
var auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
//Tenancy vars
var api_public_key = process.env.TPS_KEY;
var api_secret = process.env.TPS_SECRET
let api_url = "https://www.tpsportal.co.nz/api/v1/tenancy_application/create_property"
function sign(endpoint, key, secret, date, body) {
const encoded = new
Buffer([endpoint,body,date].join('\n')).toString('base64');
return crypto
.createHash('sha256')
.update(encoded + '+' + secret, 'utf8')
.digest()
.toString('hex');
}
//Get Property Details
const url = `https://api.getpalace.com/Service.svc/RestService/v2AvailableProperties/JSON`
const response = EleventyFetch(url, {
duration: "1d",
type: "json",
fetchOptions: {
headers: {
accept: 'application/json',
'content-type': 'application/json',
'Authorization': auth
}
}
})
const properties = await response;
//Create URLs for Image call
let propertyImageUrls = []
properties.forEach(property => {
propertyImageUrls.push(`https://api.getpalace.com/Service.svc/RestService/v2AvailablePropertyImagesURL/JSON/${property.PropertyCode}`)
});
let allData = [];
//Fetch Property Images
const allPropertyImages = await Promise.all(propertyImageUrls.map(async (url,i) => {
const imageResponse = await EleventyFetch(url, {
duration: "1d",
type: "json",
fetchOptions: {
headers: {
accept: 'application/json',
'content-type': 'application/json',
'Authorization': auth
}
}
});
let tpData;
const imageData = await imageResponse;
//Add property matching details + image to allData array
let apibody = JSON.stringify({
client_code: "8754",
property_code: properties[i].PropertyCode,
agent_name: properties[i].PropertyAgent.PropertyAgentFullName,
agent_email: properties[i].PropertyAgent.PropertyAgentEmail1,
unit: properties[i].PropertyUnit,
street_number: properties[i].PropertyAddress1,
street_name: properties[i].PropertyAddress2,
suburb: properties[i].PropertyAddress3,
city: properties[i].PropertyAddress4,
postcode: properties[i].PropertyFeatures.PropertyPostCode
})
console.log("API BODY " + i +" :",apibody)
var api_date = new Date().toISOString();
var signature = sign(api_url,api_public_key,api_secret,api_date,apibody)
console.log('hello')
let tpResponse = await EleventyFetch(api_url, {
duration: "1d",
type: "json",
fetchOptions: {
method: 'post',
headers: {
accept: 'application/json',
'content-type': 'application/json',
'X-API-DATE': api_date,
'X-API-KEY': api_public_key,
'X-API-SIGNATURE': signature
},
body: apibody
}
})
console.log("tpResponse: ", apibody)
tpData = await tpResponse;
console.log("tpData: ", tpData)
这是错误:tpData只是为每个属性提供了相同的链接,而不是不同的链接
allData.push([properties[i], imageData, tpData])
}))
// console.log(allData)
return allData;
}
module.exports = getPropertyData;
1条答案
按热度按时间w6lpcovy1#
解决方案:设置持续时间:“0”
更新任何人试图运行循环或承诺。所有
11 ty Fetch缓存根据url获取数据。
如果您在11 ty Fetch调用中将持续时间设置为任何超过“0 s”的值,则所有后续到同一URL的获取数据都不会运行,除非距离上次调用已过去一天或更长时间。
所以我所要做的就是用“0”和Promise替换“1 d”。all/for循环可以工作。