将数据从API推送到Eleventy中的现有页面(组合数据源)
我正在尝试将外部URL添加到页面的正面 - >将URL发送到刮板 - >有刮刀占用元数据属性 - >将元数据归因于佩奇的前提。
到目前为止,刮板正在工作,但是我如何能够将这些数据推到页面上?获取错误:
TemplateContentRenderError was thrown
[eleventy:dev] > (./src/index.njk)
[eleventy:dev] TypeError: Cannot read property 'slice' of undefined
这是我拥有的当前代码:
config.addCollection('products', collection => {
// This is typical Collection by Tag call
const products = collection.getFilteredByTag('products');
// Map over all the posts
const postsWithUpdates = products.map(item => {
// get URL from each page and pass it into scraper
const itemMeta = getMetaData(item.data.refUrl).then((data) => {
// map each page data with data from the scraper
item.description = data.description;
item.Image = data.icon;
// item.date = item.data.post ? new Date(item.data.post.date) : item.date
console.log(item.description);
return item;
});
console.log(itemMeta);
return itemMeta;
});
return postsWithUpdates;
});
当我console.log itemmeta
或postswithupdates
它返回为Promise {< pernew> }
所有洞察力都非常感谢,谢谢
I am trying to take an external URL added into a page's front matter --> send URL to scraper --> have scraper take metadata attributes --> push metadata attributes back to page's front matter.
So far the scraper is working, but how would I be able to push this data to the page? Getting the error:
TemplateContentRenderError was thrown
[eleventy:dev] > (./src/index.njk)
[eleventy:dev] TypeError: Cannot read property 'slice' of undefined
Here is the current code that I have:
config.addCollection('products', collection => {
// This is typical Collection by Tag call
const products = collection.getFilteredByTag('products');
// Map over all the posts
const postsWithUpdates = products.map(item => {
// get URL from each page and pass it into scraper
const itemMeta = getMetaData(item.data.refUrl).then((data) => {
// map each page data with data from the scraper
item.description = data.description;
item.Image = data.icon;
// item.date = item.data.post ? new Date(item.data.post.date) : item.date
console.log(item.description);
return item;
});
console.log(itemMeta);
return itemMeta;
});
return postsWithUpdates;
});
When I console.log itemMeta
or postsWithUpdates
it returns as Promise { <pending> }
Any and all insight is very much appreciated, thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是同步代码和异步代码与 Promise 的混合。如果您不熟悉 Promise,MDN 有一个 很棒的指南兑现承诺。
本质上,Promise 是一种在任务完成后运行一些代码的方法。当您等待另一个进程(例如网络请求)完成时,这非常有用。
这里,
getMetaData()
返回一个 Promise,您可以在其中处理then
处理程序中的数据。但是,之后的代码(console.log 和返回)不会等待任务完成,这就是为什么您会看到Promise {; }
注销。要解决此问题,您需要切换到
async
/await
语法。幸运的是,Eleventy 支持异步
addCollection
中的函数。了解有关
Promise.all< 的更多信息/code>
或
异步
函数。The issue here is the mixing of synchronous code and asynchronous code with promises. If you're not familiar with promises, MDN has a great guide on promises.
Essentially, a Promise is a way to run some code later after a task has been completed. This is helpful when you're waiting for another process, such as a network request, to finish.
Here,
getMetaData()
returns a Promise, where you can handle the data in thethen
handler. However, the code after that (the console.log and return) do not wait for the task to complete, which is why you seePromise { <pending> }
logged out.To fix this, you'll want to switch to
async
/await
syntax. Luckily for us, Eleventy supportsasync
functions inaddCollection
.Learn more about
Promise.all
orasync
functions.