所以我试图非常简单地从Firebase数据库中读取和更新数据,它给了我一个错误。
“错误:无法处理请求”
我按照说明从Google的tutorial初始化我的应用
我按照建议firebase deploy --only functions
部署我的方法
我收到以下回复:
i deploying functions
i functions: ensuring necessary APIs are enabled...
✔ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...
i functions: packaged functions (63.07 KB) for uploading
✔ functions: functions folder uploaded successfully
i functions: current functions in project: addMessage, helloWorld, now, weather, weather2
i functions: uploading functions in project: weather
i functions: updating function weather...
✔ functions[weather]: Successful update operation.
Function URL (weather): https://us-central1-*****.cloudfunctions.net/weather
有趣的是,他们的示例方法“addMessage”工作正常:
// Take the text parameter passed to this HTTP endpoint and insert it into the
// Realtime Database under the path /messages/:pushId/original
exports.addMessage = functions.https.onRequest((req, res) => {
// Grab the text parameter.
const original = req.query.text;
// Push the new message into the Realtime Database using the Firebase Admin SDK.
admin.database().ref('/messages').push({original: original}).then(snapshot => {
// Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
res.redirect(303, snapshot.ref);
});
});
但是,当我尝试使用此tutorial来更新数据而不是推送数据时,我会遇到问题。我非常感谢任何有关此问题的帮助,我已经在这工作了几个小时。< / p>
我的数据结构如何:
- 我的代码:
exports.weather = functions.https.onRequest((request, response) => {
admin.database.ref('/weather').once('value').then(snapshot => {
var temp = snapshot.child('temp').val();
var time = snapshot.child("time").val()
var diff = diff_minutes(Date().getTime(), time)
//If the last request >=5 minutes, call DarkSky API & Update database
if (diff >= 5) {
var updates = {
time: NEW_TIME,
temp: NEW_TEMPERATURE
}
snapshot.update(updates)
response.send(updates);
}
else {
response.send(temp)
}
})
});
function diff_minutes(second_time, first_time) {
var diff =(second_time - first_time) / (1000 * 60);
return Math.abs(Math.round(diff));
}
有什么想法吗?
答案 0 :(得分:3)
此代码更新数据库并发送响应不正确:
snapshot.update(updates);
response.send(updates);
snapshot
是DataSnapshot的一个实例,没有update()
方法。
将这两个陈述替换为:
snapshot.ref.update(updates).then(() => {
response.send(updates);
}).catch(error => {
console.error('Update failed:', error);
response.send('Update failed'); // or some error status of your choice
});
此外:
将admin.database.ref
更改为admin.database().ref
将Date().getTime()
更改为Date.now()
答案 1 :(得分:0)
以下是我使用Dark Sky API的最终代码(感谢所有帮助过的人!):
此代码在HTTP请求中检查最后一次API调用Dark Sky API对特定位置的请求的时间,然后在最后5分钟内使用之前的结果,或者如果更长时间更新结果。
exports.weather = functions.https.onRequest((req, res) => {
admin.database().ref('/weather').once('value').then(snapshot => {
var temp = snapshot.child('temp').val();
var time = snapshot.child("time").val();
var diff = diff_minutes(Date.now(), time);
var current = {
time: time,
temp: temp
}
if (diff >= 5) {
DarkSkyApi.loadCurrent(position).then(result => {
var updates = {
time: result.time,
temp: result.temperature
}
snapshot.ref.update(updates).then(() => {
res.send(current);
}).catch(error => {
console.error('Update failed:', error);
res.send('Update failed'); // or some error status of your choice
});
});
}
else {
res.send(current);
}
});
});
function diff_minutes(second_time, first_time) {
var diff =(second_time - first_time) / (1000 * 60);
return Math.abs(Math.round(diff));
}