Firebase函数:函数未定义

时间:2018-07-11 23:48:09

标签: javascript node.js firebase google-cloud-functions

在index.js中,我有:

exports.makeUppercase = functions.database.ref('/messages/{pushId}/original').onCreate((snapshot, context) => {
  // Grab the current value of what was written to the Realtime Database.
  const original = snapshot.val();
  console.log('Uppercasing', context.params.pushId, original);
  const uppercase = original.toUpperCase();
  // You must return a Promise when performing asynchronous tasks inside a Functions such as
  // writing to the Firebase Realtime Database.
  // Setting an "uppercase" sibling in the Realtime Database returns a Promise.
  return snapshot.ref.parent.child('uppercase').set(uppercase);
});

我确实从以下位置复制粘贴了代码: their doc

以某种方式,当我使用

进行部署时
firebase deploy --only functions

我得到:

  

错误:解析函数触发器时发生错误。   ReferenceError:函数未定义       在对象。 (/ home / [USERNAME HERE] /functions/index.js:1:87)       在Module._compile(module.js:643:30)       在Object.Module._extensions..js(module.js:654:10)       在Module.load(module.js:556:32)       在tryModuleLoad(module.js:499:12)       在Function.Module._load(module.js:491:3)       在Module.require(module.js:587:17)       在要求时(internal / module.js:11:18)       在/usr/local/nvm/versions/node/v8.9.4/lib/node_modules/firebase-tools/lib/triggerParser.js:21:11       在对象。 (/usr/local/nvm/versions/node/v8.9.4/lib/node_modules/firebase-tools/lib/triggerParser.js:75:3)

这是怎么回事,我该如何解决?

1 个答案:

答案 0 :(得分:1)

setup documentation所述,您需要导入所需的模块并初始化应用程序:

generate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            num = Integer.parseInt(n.getText().toString());
            min = Integer.parseInt(lb.getText().toString());
            max = Integer.parseInt(ub.getText().toString());

            // This will be the Key String we store in SharedPrefs
            String keyString = "";
            // This will be the Value String we store in SharedPrefs
            String valueString = "";

            for (int i = 1; i <= num; i++) {

                Random random = new Random();
                generatednum = String.format("%04d", random.nextInt(max + 1 - min) + min);

                // Append the generated Key to the keyString
                keyString = keyString + generatednum + "\n";

                char[] chars1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
                StringBuilder sb1 = new StringBuilder();
                Random random1 = new Random();
                for (int j = 0; j < length; j++)
                {
                    char c1 = chars1[random1.nextInt(chars1.length)];
                    sb1.append(c1);
                }
                random_string = sb1.toString();

                // Append the generated Value to the valueString
                valueString = valueString + random_string + "\n";
            }

            // Store our keyString and valueString in SharedPrefs
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString("key", keyString);
            editor.putString("value", valueString);
            // Don't forget to commit your changes to SharedPrefs!
            editor.commit();
        }
    });