我有一个最初由某人创建的post对象,然后将触发Firebase函数,然后将其添加到Algolia中以供以后查询。当用户删除帖子时,从Algolia中删除对象时出现一些问题。该功能已在Firebase中触发,但它是“更新”帖子,而不是删除帖子。
以下是Firebase功能控制台中的日志:
onPostWrite: Function execution took 79 ms, finished with status: 'ok'
onPostWrite: Function execution started
onPostWrite: Updated post -Lb3lcmWn1iXAdnbFDmo
以下是该函数执行的代码:
exports.onPostWrite = functions.database.ref('/posts/{postId}').onWrite((snapshot, context) => {
const index = client.initIndex(ALGOLIA_INDEX_POSTS);
//When the data is first created...
if (snapshot.before.exists()) {
const post = snapshot.before.val();
post.objectID = context.params.postId;
console.log('Updated post ', post.objectID);
return index.saveObject(post);
}
//When data has been deleted...
if (!snapshot.after.exists()) {
const post = snapshot.before.val();
post.objectID = context.params.postId;
console.log('Deleted post ', post.objectID);
return index.deleteObject(post.objectID);
}
if (!snapshot.before.exists()) {
const post = snapshot.after.val();
post.objectID = context.params.postId;
console.log('Created post ', post.objectID);
return index.saveObject(post);
}
});
如您所知,它仅执行“更新”命令,而不执行create或delete命令。
我认为这可能与我的IF语句有关,但我不确定。任何帮助将不胜感激。
答案 0 :(得分:0)
您需要切换<body>
<div id="T&M" style="display: block;">
<select id="MAIN" name="category">
<option value="0">Labor</option>
<option value ="1">Materials & Equipment</option>
<option value ="2">Equipment</option>
<option value ="3">Subcontractor</option>
</select>
<div id="Labor" style="display: inline;">
<select id="laborchoices" name="labor">
<option value="laborchoices0">Supervisor</option>
<option value ="laborchoices1">Operator</option>
<option value ="laborchoices2">Rigger</option>
</select>
<div id="sup" style="display: inline;">
<br /><br />Qty: <input type='number' step='0.25' value='1'></input> EA <br /><br />
MH/Unit: <input type='number' step='0.25' value='2'></input>
Total MH: <input type='number' step='0.05' value='1'></input><br /><br />
Rate: $<input id='rate1' type='number' step='0.25' placeholder=$rate ></input>
Total: $<br /><br />
Comments: <textarea cols='30' rows='3'></textarea></input>
</div>
<div id="opr" style="display: none;">
<br /><br />Qty: <input type='number' step='0.25' value='1'></input> EA <br /><br />
MH/Unit: <input type='number' step='0.25' value='2'></input>
Total MH: <input type='number' step='0.05' value='1'></input><br /><br />
Rate: $<input id='rate2' type='number' step='0.25' placeholder=$rate ></input>
Total: $<br /><br />
Comments: <textarea cols='30' rows='3'></textarea></input>
</div>
<div id="rig" style="display: none;">
<br /><br />Qty: <input type='number' step='0.25' value='1'></input> EA <br /><br />
MH/Unit: <input type='number' step='0.25' value='2'></input>
Total MH: <input type='number' step='0.05' value='1'></input><br /><br />
Rate: $<input id='rate3' type='number' step='0.25' placeholder=$rate ></input>
Total: $<br /><br />
Comments: <textarea cols='40' rows='3'></textarea></input>
</div>
</div>
<div id="mateq" style="display: none;">
<select id="choices1" name="form_select">
<option value="mateqchoices0">Stands</option>
<option value ="mateqchoices1">Beams</option>
<option value ="mateqchoices2">Mats</option>
<option value ="mateqchoices3">Other</option>
</select>
<div id="stands" style="display: inline;">
<br /><br />DAYS: <input type='number' step='1' value='0'></input>
Qty: <input type='number' step='1' value='0'></input><br /><br />
$/Unit: <input type='number' step='0.05' value='10.00'></input>
Total: $ <br /><br />
Description: <textarea cols='40' rows='3'></textarea></input>
</div>
<div id="beams" style="display: none;">
<br /><br />DAYS: <input type='number' step='1' value='0'></input>
Qty: <input type='number' step='1' value='0'></input><br /><br />
$/Unit: <input type='number' step='0.05' value='10.00'></input>
Total: $ <br /><br />
Description: <textarea cols='40' rows='3'></textarea></input>
</div>
<div id="Mats" style="display: none;">
<br /><br />DAYS: <input type='number' step='1' value='0'></input>
Qty: <input type='number' step='1' value='0'></input><br /><br />
$/Unit: <input type='number' step='0.05' value='10.00'></input>
Total: $ <br /><br />
Description: <textarea cols='40' rows='3'></textarea></input>
</div>
<div id="othermateq" style="display: none;">
<br /><br />DAYS: <input type='number' step='1' value='0'></input>
Qty: <input type='number' step='1' value='0'></input><br /><br />
$/Unit: <input type='number' step='0.05' value='10.00'></input>
Total: $ <br /><br />
Description: <textarea cols='40' rows='3'></textarea></input>
</div>
</div>
<div id="equip" style="display: none;">
<select id="choices2" name="form_select">
<option value="equipchoices0">Trailer</option>
<option value ="equipchoices1">Other</option>
</select>
<div id="trailer" style="display: inline;">
<br /><br />DAYS: <input type='number' step='1' value='0'></input>
Qty: <input type='number' step='1' value='0'></input><br /><br />
$/Unit: <input type='number' step='0.05' value='10.00'></input>
Total: $ <br /><br />
Description: <textarea cols='40' rows='3'></textarea></input>
</div>
<div id="otherequip" style="display: none;">
<br /><br />DAYS: <input type='number' step='1' value='0'></input>
Qty: <input type='number' step='1' value='0'></input><br /><br />
$/Unit: <input type='number' step='0.05' value='10.00'></input>
Total: $ <br /><br />
Description: <textarea cols='40' rows='3'></textarea></input>
</div>
</div>
<div id="subq" style="display: none;">
<select id="choices3" name="form_select">
<option value="subq0">Per diem</option>
<option value ="subq1">Other</option>
</select>
<div id="perdiem" style="display: inline;">
<br /><br />Qty: <input type='number' step='0.25' value='1'></input> EA <br /><br />
Rate: <input type='number' step='0.05' value='160.00'></input>
Total: $ <br /><br />
Description: <textarea cols='40' rows='3'></textarea></input>
</div>
<div id="othersubq" style="display: none;">
<br /><br />Qty: <input type='number' step='0.25' value='1'></input> EA <br /><br />
Rate: <input type='number' step='0.05' value='160.00'></input>
Total: $ <br /><br />
Description: <textarea cols='40' rows='3'></textarea></input>
</div>
</div>
</div>
</body>
语句的顺序。
if
由于您要在每个块中返回,因此即使删除了第一个块,您的执行也会停止。我认为,如果您交换了前两个代码块,它将按预期工作:
// Called onUpdate, a snapshot exists before the update and exists after the update
// Called onDeletion, a snapshot exists before the update and doesn't exist after the update
if (snapshot.before.exists()) {
// Called onDeletion, nothing exists after operation
if (!snapshot.after.exists()) {
// Called onCreate. Nothing existed `before`, but it does now
if (!snapshot.before.exists()) {
注意:由于前两个将捕获// Called onDelete, nothing exists after operation
if (!snapshot.after.exists()) {
// Called onUpdate, a snapshot exists before the update and exists after the update
// Called onDelete, a snapshot exists before the update and doesn't exist after the update
// Since you've moved a "better" `onDelete` above this, this function will now only be called for onUpdate
if (snapshot.before.exists()) {
// Called onCreate. Nothing existed `before`, but it does now
if (!snapshot.before.exists()) {
和if
操作,因此您实际上并不需要最后一个delete
语句,最后一条是代码可以采用的唯一其他路径。这是我的全部看法:
update