因此,我有一个https函数,当触发该函数时,它需要读取当前服务器时间并获取其target_date小于当前时间的项目列表。一旦获得了这些项目的列表,就需要通过将其“状态”值设置为“已完成”来更新这些各个项目。我已经编写了该函数,但是它引发了错误。你能帮我解决吗?
exports.checkCampaignDate = functions.https.onRequest((request, response) => {
const current_time = Date.now();
console.log("current time", current_time);
var targetRef = admin.database().ref('/Fund');
console.log("target ref", targetRef);
targetRef.orderByChild("time_left").endAt(current_time).once("value", function(snapshot) {
const artcall_list = snapshot.val();
const artcall_id = snapshot.key;
snapshot.forEach((artcallSnap) => {
console.log("Artcall Snap", artcallSnap.val());
artcallSnap.child("status").set("completed");
});
console.log("This is the list of artcalls that have this same date", artcall_list);
console.log("This is the artcall id:", artcall_id);
// do some stuff once
});
return null;
});
我的数据库:
"Fund" : {
"-LEEy7uxXEeI4AJuePoB" : {
"amount_funded" : 5200,
"artist_name" : "Sean Roldan and Friends",
"genre" : "Indian Blues and Jazz",
"image_url" : "https://firebasestorage.googleapis.com/v0/b/artcall-f8f1a.appspot.com/o/profile_images%2Fthumbs%2Fseanroldan.jpg?alt=media&token=6414d8d6-e8a8-487e-baf3-0d36d017a205",
"percent_funded" : 0.29714285714285715,
"perf_location" : "Chennai",
"reward_amt" : 6000,
"target_amount" : 1750000,
"target_date" : 1566568176731,
"time_left" : 1566568176731,
"total_backers" : 1
"status" : "incomplete"
},
"-asdffdsa123" : {
"amount_funded" : 0,
"artist_name" : "Agam",
"genre" : "Rock",
"image_url" : "default",
"percent_funded" : 0,
"perf_location" : "Chennai",
"target_amount" : 1300000,
"target_amt" : 1300000,
"target_date" : 1566568176731,
"time_left" : 1566568176731,
"total_backers" : 0
"status" : "incomplete"
}
},
错误:
TypeError: artcallSnap.child(...).set is not a function
at snapshot.forEach (/user_code/index.js:311:36)
at /user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:4255:20
at /user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:3669:24
at LLRBNode.inorderTraversal (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:2606:13)
at SortedMap.inorderTraversal (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:3052:27)
at ChildrenNode.forEachChild (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:3668:24)
at DataSnapshot.forEach (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:4254:31)
at /user_code/index.js:309:12
at onceCallback (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:4843:51)
at /user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:4465:22
at exceptionGuard (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:691:9)
at EventList.raise (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:9727:17)
at EventQueue.raiseQueuedEventsMatchingPredicate_ (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:9681:41)
at EventQueue.raiseEventsForChangedPath (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:9665:14)
at Repo.onDataUpdate_ (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:12770:26)
at PersistentConnection.onDataPush_ (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:12070:18)
at PersistentConnection.onDataMessage_ (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:12064:18)
at Connection.onDataMessage_ (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:11341:14)
at Connection.onPrimaryMessageReceived_ (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:11335:18)
at WebSocketConnection.onMessage (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:11236:27)
at WebSocketConnection.appendFrame_ (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:10841:18)
at WebSocketConnection.handleIncomingFrame (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:10891:22)
答案 0 :(得分:1)
artcallSnap
是DataSnapshot
,没有set()
方法。要从快照中获取Reference
,请使用其ref
属性。所以:
artcallSnap.ref.child("status").set("completed")