json—为什么我不能从javascript中的api获取数据?它显示部署已成功创建但在firebase云函数中运行时,它不起作用

cbjzeqam  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(270)

我想通过firebase云功能实现firebase云消息传递,这样我就可以向安装了我的android应用程序的每个用户发送消息。该消息将是来自此url的json对象的键的值<[https://coronavirus-19-api.herokuapp.com/countries/bangladesh]>.
单击下面的文本以查看json对象的图像
在我的例子中,我想通过firebase云消息发送键“todaydeaths”的值。当我试图通过api方法调用json并获取它的数据并在index.js文件的消息中设置它时,它没有显示任何错误并创建了deploy。但是当我尝试检查函数的状态时,它显示错误。
单击下面的文本查看firebase cloud函数日志中显示的错误图像。firebase云函数日志。
我将在下面添加index.js代码:

const functions = require("firebase-functions");
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.scheduledFunction = functions.pubsub.schedule('every 2 minutes').onRun(async (context) 
=> {

const response=await axios.get(
    'https://coronavirus-19-api.herokuapp.com/countries/bangladesh'
);
console.log(response.data);
const body=response.todayDeaths;

const message = {
    notification: {
        'title': 'Today death',
        'body': body,
    },
    topic:'allUser'

};

admin.messaging().send(message)
.then(response=>{
    console.log("Successfully sent",response);
}).catch(error=>{
    console.log("failed",response);
});

console.log('This will be run every 2 minutes!');
return null;
});

如何解决该问题,使firebase云消息传递成功地将消息发送给项目的所有用户?

zzzyeukh

zzzyeukh1#

看起来你只是忘了导入 axios .
将此添加到顶部:

const axios= require('axios');

相关问题