Reference.child失败:第一个参数是无效路径=“ / Users / [对象对象] / device_token”

时间:2018-11-23 18:01:51

标签: javascript firebase firebase-cloud-messaging

我正在尝试使用从教程中获得的Java脚本代码向用户发送通知

'use strict'

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

exports.sendNotification = functions.database.ref('/Notifications/{user_id}/{notifications_id}').onWrite((change, context) => {
	const user_id = change.after.val();
	const notifications = change.after.val();

	console.log('We Have A Notification for :', user_id);

	if (!change.after.val()){
		return console.log("A Notification Has Been Deleted From The Database: ", notifications_id)
	}

	const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');

	return deviceToken.then(result => {

		const token_id = result.val();

		const payload = {
			notification: {
				title: "Friend Request",
				body: "You just got a new friend request",
				icon: "default"
			}
		};
	
		return admin.messaging().sendToDevice(token_id, payload ).then(Response =>{
	
			console.log('this is the notification')
		});
	

	});


});

,但是当它尝试发送通知时,会在Firebase功能日志中返回此错误。我不知道firebase是否对他们的文档进行了更改。 请我真的需要帮助以使此代码正常工作

Error: Reference.child failed: First argument was an invalid path = "/Users/[object Object]/device_token". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"
    at validatePathString (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1612:15)
    at validateRootPathString (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1623:5)
    at Reference.child (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:13930:17)
    at Database.ref (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:15093:48)
    at exports.sendNotification.functions.database.ref.onWrite (/user_code/index.js:17:39)
    at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20)
    at /var/tmp/worker/worker.js:758:24
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)

0 个答案:

没有答案