我正在尝试按照Aaron Klasner在Hackernoon上的教程创建一个React中的提要。
为了进行本地测试,我需要绕过Medium用于阻止本地请求的CORB。
我建立了一个firebase函数,但是出现以下错误:
TypeError: Cannot read property 'replace' of undefined
at strip (/user_code/index.js:18:37)
at Request.request [as _callback] (/user_code/index.js:19:25)
at self.callback (/user_code/node_modules/request/request.js:185:22)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at Request.onRequestError (/user_code/node_modules/request/request.js:881:8)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at TLSSocket.socketErrorListener (_http_client.js:324:9)
at emitOne (events.js:96:13)
这是我从本教程中获得的firebase函数:
const functions = require('firebase-functions');
var request = require('request');
exports.medium = functions.https.onRequest((req, res) => {
if(!req.query.username) {
return res.status(400).send('Error: You need to include query param ?username=@yourUsername');
}
const url = `https://medium.com/${req.query.username}/latest?format=json`;
return request(url,(error, response, body) => {
const prefix = `])}while(1);</x>`
const strip = payload => payload.replace(prefix, ``)
res.send(JSON.parse(strip(body)));
});
})
以下也是该教程的链接:https://hackernoon.com/import-your-medium-feed-into-react-ceadbaf785c7
答案 0 :(得分:0)
您得到的错误是由正文undefined
引起的,因此请在发送回响应之前尝试检查错误:
return request(url,(error, response, body) => {
if (error) {
return console.error('Failed to fetch data: ', error);
}
const prefix = '])}while(1);</x>';
const strip = (payload) => payload.replace(prefix, '');
res.send(JSON.parse(strip(body)));
});