好的,我看过类似的问题,比如Firebase function onWrite not being called,认为得到引用是我的错,但是我不知道我的Firebase函数在这里发生了什么。
当对数据库进行写入时,我只是试图获得一个函数来写入我的数据库。我完全遵循了firebase教程:
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
//https://firebase.google.com/docs/functions/database-events
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// const gl = require('getlocation');
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
});
exports.enterLocation = functions.database.ref('/Users/{name}') //brackets is client param
.onWrite(event => {
// Grab the current value of what was written to the Realtime Database.
// const original = event.data.val();
console.log('SKYLAR HERE:', event.params.name);
// You must return a Promise when performing asynchronous tasks inside a Functions such as
return firebase.database().ref('/Users/{name}').set({ location: 'test loc' });
});函数正在运行,但是在我的日志中,我得到了一个非常无用的错误,它正在获取{name} param,而且数据肯定写到了我的数据库中,但是我的服务器代码没有编写:

我得到-
ReferenceError:在exports.enterLocation.functions.database.ref中没有定义firebase
这是没有意义的,因为它的定义。我只想在我创建的用户下添加一个额外的子程序,就像我已经使用了“密码”一样。

我做错了什么?
发布于 2017-11-21 00:01:46
这里有两个问题。首先,您还没有在代码中任何地方定义firebase。我认为您打算使用admin,而不是使用Admin。
第二,看起来您正在尝试将变量内插到字符串中,以构建ref的名称。你的语法错了。
我想您是想在最后一行代码中这样说:
return admin.database().ref(`/Users/${name}`).set({ location: 'test loc' });注意字符串引号上的回标。JavaScript语法允许您使用${exp}在字符串中插入某些表达式的内容。
实际上,这里甚至不需要使用admin。由于您试图写回触发函数的相同位置,所以只需使用来自事件对象的ref:
return event.data.adminRef.set({ location: 'test loc' });发布于 2017-11-20 23:55:18
而不是这样:
return firebase.database().ref('/Users/{name}').set({ location: 'test loc' });用这个:
return admin.database().ref('/Users/{name}').set({ location: 'test loc' });https://stackoverflow.com/questions/47402678
复制相似问题