Database Structure
Notification Structure
After successfully deploying my function into fire-base I am having these error in firebase function logs
1: Function execution took 1724 ms, finished with status: 'error'
2:TypeError: Cannot read property 'uid' of undefined
at exports.sendNotification.functions.firestore.document.onWrite.event
(/user_code/index.js:9:30)
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:733:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
3:{"@type":"type.googleapis.com/google.cloud.audit.AuditLog","status":
{"code":3,"message":"The request has errors"},"authenticationInfo":
{"principalEmail":"info.jap.123@gmail.com"},"requestMetadata"{"callerIp":"39.50.37.100",
Index.js
'use-strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
//admin.initializeApp(functions.config().firebase);
exports.sendNotification =
functions.firestore.
document("Users/{uid}/Notification/{nid}").onWrite(event=>
{
const user_id = event.params.uid
const notification_id = event.params.nid;
});
Notification Class in this class i am storing the notification id and message into Firebase-Firestore.
private void sendNotification()
{
String number = edTxtDevId.getText().toString();
if (TextUtils.isEmpty(number))
{
edTxtDevId.setError("Please Provide the dev id thank you");
return;
}
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
String time = String.valueOf(cal.getTime());
final String message = "Notification has been sent to this number "+number+" by this number "+user_id+" at time = "+time;
Map<String , Object> notificationMessage = new HashMap<>();
notificationMessage.put("From" , mUserId);
notificationMessage.put("message" , message);
for (int i = 0 ; i < userList.size() ; i++)
{
if (userList.get(i).equals(number))
{
Log.e(TAG, "sendNotification: "+number+" found " +userList.get(i) + " id = "+i);
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
mFirestore.collection("Users/"+mUserId+"/Notification").add(notificationMessage).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}
});
}
}
}
Please help me if you know how to solve these Thank you in advance!
Edited:
First of all, just as written here , You need to pass a function with 2 arguments to onWrite()
.
Here is an example .
In your case,
document("Users/{uid}/Notification/{nid}").onWrite(event=>
should be document("Users/{uid}/Notification/{nid}").onWrite((change, context) =>
So, your code should look like:
'use strict'
const functions = require('firebase-functions');
// const admin = require('firebase-admin');
// admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.firestore
.document("Users/{uid}/Notification/{nid}")
.onWrite((change, context) => { // <- changed arguments here
// console.log(uid)
// console.log(nid)
// you can try these, but i think these console.log() will not work.
});
Second, if you want to retrieve uid
and nid
from the document path, you can use functions.database.DataSnapshot.ref or functions.EventContext.resource
so your code should look like:
'use strict'
const functions = require('firebase-functions');
// const admin = require('firebase-admin');
// admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.firestore
.document("Users/{uid}/Notification/{nid}")
.onWrite((change, context) => {
// console.log(change.ref)
// the output will be like: "Users/PfUyNf.../Notification/0Ujdt..."
// or
// console.log(context.resource)
// the output will be like:
// "projects/_/instances/<databaseInstance>/refs/Users/PfUyNf.../Notification/0Ujdt..."
});
then use regexp or something to process the output.
I think there will be better solutions, but hope it helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.