I tried creating one program which fetch data from firebase database from node js server.
function FCM()
{
var payload = {
data: {
//Some data
}
};
var db = admin.database();
var ref = db.ref("PATH_TO_DATABASE");
ref.on("value", function(snapshot) {
registrationToken = REGISTRATION TOKEN
admin.messaging().sendToDevice(registrationToken, payload)
.then(function(response) {
console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
}
But when this function gets completed then the server keeps on running as usual which is fine but the problem is that when any data gets deleted from PATH_TO_DATABASE then it gives error:
TypeError: Cannot convert undefined or null to object
Now I don't want firebase to keep on checking the value to that path. I just want to free that thing. So how I can do it ?
ref.on("value") is going to trigger whenever that value changes, like when the data is deleted. Instead use
ref.once("value", function(snapshot)
Then admin.messaging().sendToDevice(registrationToken, payload) won't get triggered again.
Related
I'm using the serverless apigateway websockets and I can successfully get messages sent back and forth between the lambda function and client.
However I can't figure out how to get my function to send only a single message. It's currently sending two messages due to the callback at the end of the function. This is more of a nodejs issue, but I've been trying for the past couple of hours to figure out how, but can't seem to.
var params2 = {
TableName: "UserConnections",
FilterExpression: "cameraId = :val",
ExpressionAttributeValues: { ":val": {"S" : JSON.parse(event.body).data.camera_id}}
};
DDB.scan(params2, function(err, data) {
if (err) throw err;
console.log("DATA: " + JSON.stringify(data));
for(var i = 0; i<data['Items'].length; i++){
var id = data['Items'][i]['connectionId'].S;
console.log("List of connection ids: " + id);
var params3 = {
ConnectionId: id,
Data: JSON.stringify(message)
};
apigatewaymanagementapi.postToConnection(params3, function(err, data) {
if (err){
throw err; // an error occurred
}else{
console.log("Success sending message to clients: " + JSON.stringify(data));
}
});
}
});
callback(null, {
statusCode: 200,
body: "Message Processed in Lambda!"
});
In postToConnection method, it sends a message back to multiple users, and the callback function sends the body to the same users. How can I just send the params3 back to the users and not use the callback to end the function
Edit1:______________________________________________
Adding
callback(null, {});
Still sends two messages except the second one is now empty. How can I get it to strictly send only one message
Return with a empty object, the return value is ignored when this function is invoked from WebSocket gateway
return {}; // callback(null, {});
I need to store my values from the request body to the cloud firestore and sent back the foruminsertdata.Name back in the response. But I am not able to do this.
const functions = require('firebase-functions');
const admin =require("firebase-admin");
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
exports.helloWorld = functions.https.onRequest((req, res) => {
if(req.method === 'POST'){
foruminsertdata = req.body;
db.collection('forum').add({
Name: foruminsertdata.Name,
Description: foruminsertdata.Description,
Heading: foruminsertdata.Heading,
PostedOn: foruminsertdata.PostedOn,
Status: foruminsertdata.Status,
})
.then(ref => {
console.log('Added document with ID: ', ref.id);
return res.status(200).json(
{
message: foruminsertdata.Name
});
})
.catch(err => {
console.log('Error getting documents', err);
});
res.json({
message: foruminsertdata.Status,
});
}
})
I don't know what is happening...Whatever I do I always get the output as
{
message: foruminsertdata.Status,
}
in which "foruminsertdata.Status" has some value that I give
but what I expect the output as
{
message: foruminsertdata.Name
}
Your function is immediately returning foruminsertdata.Status to the client without waiting for the promise from the database operations to resolve. Any function that returns a promise is asynchronous and returns immediately. Execution will continue in the callbacks you attach to it.
I'm not sure why you have two calls to res.json() in your code, but if you want to send a response only after your query completes, you'll remove the second one and just send a response after the query is done. You will probably also want to send a response in the catch callback as well to indicate an error.
I'm using request-promise to get data from an endpoint that I have.
Is it posible to 'capture' a json response in a variable to use it anywhere?
try{
var gamer = '';//variable to capture json data
var options = {
uri: 'http://localhost:4000/gamers/'+gamer._id+'/find',
json: true
};
RequestPromise(options)
.then(function (data) {
gamer = data;//capturing response
})
.catch(function (err) {
console.log("Error saving player data !");
});
.... do something with gamer ....
}catch(err){
res.status(500).send({
message: err.message || 'An error occurred generating player teams !'
});
}
The reason that I need to do this is because actually I don't have access to the database to get that information, so my only option is to consume an API to get information through collections id's.
Your doing a lot of things correctly already. The issue is your gamer variable will get assigned the value you expect first when your promise resolves. There are many ways to skin this cat, but to get you started try performing whatever you want to perform on the gamer variable in .then(), like this:
try{
var gamer = '';//variable to capture json data
var options = {
uri: 'http://localhost:4000/gamers/'+gamer._id+'/find',
json: true
};
RequestPromise(options)
.then(function (data) {
gamer = data;//capturing response
// here is the rigth place perofrm operations on the answer, as this part of the code gets executed after promise reolves. BTW. Then you don't need variable gamer.
})
.catch(function (err) {
console.log("Error saving player data !");
});
// here is not the right place to do something with gamer as this is executed as soon as promise is initialized, not as it resolves. This means your variable will have initial value here
}catch(err){
res.status(500).send({
message: err.message || 'An error occurred generating player teams !'
});
}
A few months ago i started using Firebase's backend tools. they're awesome. So i had to move our current method of notifying users of their to-do list. We moved to Cloud Functions and it was 40x faster. Until a week ago i got a fresh copy of MacOS i had to delete everything in my Solid State Drive. When i did i also installed the firebase command line tools. I had my code which i used to notify users, it suddenly stopped working
Here's what happens:
When someone adds a new record to our daily list. the app notifies other family members about the new record. So i had to come up with a method to do so..
OLD CODE (worked seamlessly)
const functions = require('firebase-functions');
var admin = require("firebase-admin");
admin.initializeApp();
var registrationToken = 'egV-C3ItwJE:APA91bGf5ezSRQQFHkzCNzfqhS6tiKRbs6IXXs57DFTrNCWRrVY0pZ4PHsK8G3ZjvvvO4JCvd13j_jBcJkgRh06YJ5Jw6tohc81Ro0k4HdHG-Jlv4sbW5t1DNmJBDeGf48l05eDlfMGO';
var payload = {
data: {
title: 'TEST/2',
body: 'TEST/2'
}
};
// registration token.
admin.messaging().sendToDevice(registrationToken, payload)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response, payload);
return
})
.catch((error) => {
console.log('Error sending message:', error);
});
Back in my previous version of macOS this code had no issues. upgrading Firebase Command Line tools i had to make some edits to the code:
NEW CODE (NOT WORKING)
// This registration token comes from the client FCM SDKs.
var registrationToken = 'egV-C3ItwJE:APA91bGf5ezSRQQFHkzCNzfqhS6tiKRbs6IXXs57DFTrNCWRrVY0pZ4PHsK8G3ZjvvvO4JCvd13j_jBcJkgRh06YJ5Jw6tohc81Ro0k4HdHG-Jlv4sbW5t1DNmJBDeGf48l05eDlfMGO';
// See documentation on defining a message payload.
var message = {
data: {
title: '850',
body: '2:45'
},
token: registrationToken
};
// Don't use the legacy sendToDevice
admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
return
})
.catch((error) => {
console.log('Error sending message:', error);
});
The Problem is:
1- When this function hits. i get the "Successfully sent message:" response. but the message won't reach the device
Things i have tried:
1- The device token is correct and i have tested it using the manual push notification on single devices
2- The device receives notifications.
3- The device runs on iOS 11.1 same as before
Thanks
Can u check with this function,I had tried with this I am able to get notifications.
In you first code sample u had user sendToDevice but in second sample you had used send
function sendPushNotification(fcmtoken, notificationTitle, notificationMessage) {
if (fcmtoken != null && fcmtoken != "" && fcmtoken != " ") {
var payload = {
notification: {
title: notificationTitle,
body: notificationMessage,
sound: "default"
}
};
admin.messaging().sendToDevice(fcmtoken, payload)
.then(function (response) {
}).catch(function (error) {
console.log("Error sending message:", error);
});
}
}
I've developed a nodeJS API (using express) which allow users to login and get a list of files that they have stored in a remote server. And as you understand, the code must be non-blocking so the webserver can still responds to logging in requests, even if there are some users fetching theirs files lists.
Every time a user make a request to get his files list, the listOfFiles function is called.
This is the code:
exports.listOfFiles = function(req,res){
db.Account.find({where: {id:1}}).then(function(newAcc){
console.log("encontrou a account");
getFiles('/', newAcc.accessToken, '0', newAcc, function(error){
if (error) {
log.error('Error getting files');
}else{
console.log("callback!")
}
});
});
}
getFiles function: this function is responsible for fetching the file list from the remote server, and store them in a postgres database
function getFiles(path, accessToken, parentID, newAcc, callback){
var client = new ExternalAPI.Client({
key: config.get("default:clientId"),
secret: config.get("default:clientSecret")
});
client._oauth._token = accessToken;
var options = {
removed : false,
deleted : false,
readDir: true
}
//this is the instruction that fetch an array of items
//(metadata only) from a remote server
client.stat(path, options, function(error, entries) {
if (error) {
if (error.status == 429) {
console.log(accessToken + 'timeout')
setTimeout(
getFiles(path, accessToken, parentID, callback),
60000);
}else{
log.error(error);
callback(error,null);
}
}
else {
//When the array os items arrives:
console.log("RECEIVED FILES")
var inserted = 0;
var items = entries._json.contents;
for(var file in items){
var skyItemID = uuid.v1();
var name = items[file].path.split('/').pop();
var itemType;
if (items[file].is_dir) {
itemType = 'folder';
}else{
itemType = 'file';
}
newAcc.createItem({
name : name,
lastModified: items[file].modified,
skyItemID: skyItemID,
parentID: parentID,
itemSize: items[file].bytes,
itemType : itemType,
readOnly: items[file].read_only,
mimeType: items[file].mime_type
}).then(function(item){
console.log(item.name)
if (++inserted == items.length) {
console.log(inserted)
console.log(items.length)
console.log("callsback")
callback();
}
}).catch(function(error){
log.error('[DROPBOX] - Filename with special characters');
callback(new Error);
return;
});
}
}
});
}
The problem here is, the moment that webserver prints console.log("RECEIVED FILES") in our console, it stops responding to all other requests, such as log in or fetch files requests from other users.
And it starts responding again when it prints console.log("callback!"). So, i'm assuming that somehow nodeJS is blocking itself until getFiles function is finished and called back.
I think that this is not a normal behaviour. Shouldn't nodeJS be responding to responds to other requests even if there are some operations running in background? Shouldn't getFiles function being run in background and not affecting/blocking all other requests? What am I doing wrong?
Thanks!
I am facing the same kind of problem for long time server http request blocks the service for response other client requests. This is my topic. What is the correct behavior for Node.js while Node.js is requesting a long time http request for other servers Currently, I got no answer for that. If you got the answer, please reply me. Thanks.