我正在尝试创建一个计划的云函数,该函数使用api调用中的值更新文档字段。但是我在日志中遇到了上面提到的错误,我应该返回一个诺言(据我所知)
任何帮助都将得到
api调用返回此json:
{ "alarm": "false" }
我的代码:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as rp from 'request-promise';
admin.initializeApp()
export const scheduledAlarmUpdate = functions.pubsub.schedule('every 30 minutes').onRun((context) => {
const docRef = admin.firestore().collection('alarms').doc('stuttgart');
const username = 'bitfactory';
const pass = '...';
const options = {
url: 'https://api.bitfactory.io/fineparticlesalarm/',
auth: {
user: username,
password: pass
},
method: 'GET',
json: true
}
return rp(options).then(data => {
console.log(data.alarm)
docRef.set({
alarm: data.alarm
})
.catch(err => {
console.log(err);
});
})
});
答案 0 :(得分:1)
您需要返回set()
方法返回的promise,如下所示:
export const scheduledAlarmUpdate = functions.pubsub
.schedule('every 30 minutes')
.onRun(context => {
const docRef = admin
.firestore()
.collection('alarms')
.doc('stuttgart');
const username = 'bitfactory';
const pass = '...';
const options = {
url: 'https://api.bitfactory.io/fineparticlesalarm/',
auth: {
user: username,
password: pass
},
method: 'GET',
json: true
};
return rp(options)
.then(data => {
console.log(data.alarm);
return docRef.set({ // ->>> here return
alarm: data.alarm
});
})
.catch(err => {
console.log(err);
});
});