中的非法返回语句

中的非法返回语句

本文介绍了JavaScript 中的非法返回语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有超过 1 个子节点的天"节点不会被删除.我该如何解决这个问题?

我需要确保我的承诺冒泡到顶层的最后一个 then().所以我需要在 collectionRef.once 之前返回.但是该 return 语句现在阻止了 collectionRef.once 的发生.我被困住了!

这是我的代码

const functions = require('firebase-functions');const admin = require('firebase-admin');admin.initializeApp(functions.config().firebase);常量 defaultDatabase = admin.database();export.deleteOldItems = functions.database.ref('/path/to/items/{pushId}').onWrite(事件 => {var ref = event.data.ref.parent;//引用项目var now = Date.now();var cutoff = 现在 - 2 * 60 * 60 * 1000;var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);返回 oldItemsQuery.once('value', function(snapshot) {//创建一个包含所有需要移除的子元素的地图变量更新 = {};快照.forEach(函数(子){更新[child.key] = null});//一次性执行所有更新并返回结果结束函数返回 ref.update(更新);}).then(函数() {;常量 theRef = event.data.ref;const collectionRef = theRef.parent.child('days');返回集合参考;//非法退货声明collectionRef.once('value').then(messagesData => {if(messagesData.numChildren() > 1) {让更新 = {};更新['/天'] = null;返回 defaultDatabase.ref().update(updates);//'days' 不会被删除,即使它有超过 1 个孩子(如图所示)!}})});});

数据结构:https://i.stack.imgur.com/gVn8S.jpg

解决方案
exports.deleteOldItems = functions.database.ref('/path/to/items/{pushId}').onWrite(事件 => {var ref = event.data.ref.parent//对项目的引用var now = Date.now()var cutoff = 现在 - 2 * 60 * 60 * 1000var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff)返回 oldItemsQuery.once('value', function(snapshot) {//创建一个包含所有需要移除的子元素的地图变量更新 = {}快照.forEach(函数(子){更新[child.key] = null})//一次性执行所有更新并返回结果结束函数返回 ref.update(更新)}).then(函数() {//const theRef = event.data.refconst collectionRef = defaultDatabase.ref().child('/days')//return collectionRef//非法返回声明collectionRef.once('value').then(messagesData => {console.log(`你好 messageData : ${messagesData.numChildren()}`)if(messagesData.numChildren() > 1) {常量更新 = {}更新['/天'] = null返回 defaultDatabase.ref().update(updates);//'days' 不会被删除,即使它有超过 1 个孩子(如图所示)!}})})

使用 defaultDatabase.ref().child('/days') 而不是使用 event.data.ref.parent

还请阅读文档并了解 Promise 的工作原理,它将对您的未来有所帮助.目前,这些更改将起作用.在我结束时进行了测试.

您必须观看的视频

event.data.ref 和 event.data.adminRef 有什么区别?- #AskFirebase使用 Cloud Functions for Firebase 进行异步编程(我保证!)

您可以订阅他们的 Firebase YouTube 频道以获取最新更新并了解更多信息.p>

A 'days' node with more than 1 child isn't getting removed. How can I fix this issue?

I need to ensure that my promise bubbles up to the last then() on the top-level. So I need a return before collectionRef.once. But that return statement now prevents the collectionRef.once from happening. I'm stuck!

Here's my code

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

exports.deleteOldItems = functions.database.ref('/path/to/items/{pushId}')
.onWrite(event => {
  var ref = event.data.ref.parent; // reference to the items
  var now = Date.now();
  var cutoff = now - 2 * 60 * 60 * 1000;
  var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
  return oldItemsQuery.once('value', function(snapshot) {
    // create a map with all children that need to be removed
    var updates = {};
    snapshot.forEach(function(child) {
      updates[child.key] = null
    });
    // execute all updates in one go and return the result to end the function
    return ref.update(updates);
  }).then(function() {;

    const theRef = event.data.ref;
    const collectionRef = theRef.parent.child('days');
    return collectionRef; // ILEGAL RETURN STATEMENT
    collectionRef.once('value').then(messagesData => {
        if(messagesData.numChildren() > 1) {

  let updates = {};
updates['/days'] = null;
return defaultDatabase.ref().update(updates); // 'days' doesn't get removed even if it has more than 1 child (as in the image)!
        }
    })
});

});

Data structure: https://i.stack.imgur.com/gVn8S.jpg

解决方案
exports.deleteOldItems = functions.database.ref('/path/to/items/{pushId}')
  .onWrite(event => {
    var ref = event.data.ref.parent // reference to the items
    var now = Date.now()
    var cutoff = now - 2 * 60 * 60 * 1000
    var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff)
    return oldItemsQuery.once('value', function(snapshot) {
      // create a map with all children that need to be removed
      var updates = {}
      snapshot.forEach(function(child) {
        updates[child.key] = null
      })
      // execute all updates in one go and return the result to end the function
      return ref.update(updates)
    }).then(function() {
      // const theRef = event.data.ref
      const collectionRef = defaultDatabase.ref().child('/days')
      // return collectionRef // ILEGAL RETURN STATEMENT
      collectionRef.once('value').then(messagesData => {
                console.log(`Hello messageData : ${messagesData.numChildren()}`)
              if(messagesData.numChildren() > 1) {
                    const updates = {}
                    updates['/days'] = null
                    return defaultDatabase.ref().update(updates); // 'days' doesn't get removed even if it has more than 1 child (as in the image)!
                }
      })
  })

use defaultDatabase.ref().child('/days') instead of using event.data.ref.parent

also please go through the documentation and learn how promises works it will help you future. For now these changes will work.Tested at my end.

videos you must watch

What's the difference between event.data.ref and event.data.adminRef? - #AskFirebaseAsynchronous Programming (I Promise!) with Cloud Functions for Firebase - Firecasts

you can subscribe their Firebase YouTube Channel to get latest updates and learn More.

这篇关于JavaScript 中的非法返回语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 11:00