Open strugee opened 7 years ago
You can simply bind
the this
context to the function.
file.data.posts.map(function(post) {
return {
title: post.title,
url: urlPrefix + file.relative,
categories: post.categories,
// TODO: normalize URLs to absolute URLs
// See the `rss` docs for details
description: post.contents,
date: new Date(post.time.epoch * 1000)
};
}).forEach(feed.item.bind(feed));
I have a static site generator which, shockingly, is using node-rss to output RSS feeds.
This code runs successfully:
As I'm sure you've guessed,
file.data.posts
is an array of posts. All that this does is map each post in to a node-rss-compatible item object and then call.item()
on each object.This code is theoretically functionally equivalent:
but it fails with
TypeError: Cannot read property 'items' of undefined
. Note thatfeed.item
is being passed directly intoforEach
- presumably this is manglingthis
somehow, causing the error.Here's the full stack trace:
I'm on Node.js@4.4, node-rss@1.2.1.