Express in nodejs is keeping cache with cloud functions
Well I have my functions that query to firestore in nodejs and i am using express.
So i have a function that returns a user by the email provided from android.
Function in android that pass the email to nodejs
Function in nodejs that query to firestore to get user by email
I use this function to log in the user on my app and its works fine.
But the problem comes when to delete a user i use too the function getUserByEmail to get the user that i have to delete. I do not understand why but when i request the function getUserByEmail on the delete function it is returning the same value as if i were logging again. I do not know if i have to route my functions on nodejs or disable the cache.
Whatever it is i do not know how to do it and if you can explain me or paste a link about best practices to set up express in js and google cloud functions it would be a great help.
This is how i initialize the app express on my indexjs Despite having disable cache it is not working. Thanks!
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const Firestore = require('@google-cloud/firestore');
//const service_account = require(getKeyFileName());
const express = require('express');
const body_parser = require('body-parser');
const cors = require('cors');
const router = express.Router();
let app = express();
app.use(cors(origin: true));
app.use(body_parser.json());
app.use(body_parser.urlencoded( extended: true ));
app.use((req, res, next) =>
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next();
);
const firestore = new Firestore(
projectId: getProyectId(),
keyFilename: getKeyFileName()
);
const collection_userRef = firestore.collection('users/');
let user_model =
userName: "",
userId: "",
userScore: 0,
first_login: true,
section: "",
userEmail: ""
;
let user = null;
main();
// admin.initializeApp(
// credential: admin.credential.cert(service_account)
// );
function main()
admin.initializeApp(functions.config().firebase);
userDao();
function userDao()
getUserByEmail();
exports.getUserByEmail = functions.https.onRequest(app);
postUser();
exports.postUser = functions.https.onRequest(app);
getUsers();
exports.getUsers = functions.https.onRequest(app);
deleteUser();
exports.deleteUser = functions.https.onRequest(app);
function getUsers()
app.get('', (req, res) =>
const users = ;
return new Promise((resolve, reject) =>
return collection_userRef.get().then(snapshot =>
snapshot.forEach(doc =>
console.log(doc.data());
users.push(doc.data());
);
resolve(users);
res.send(users);
return users;
).catch(error =>
console.log(error.message);
reject(error);
);
);
);
//using post to put user and to update it
function postUser()
app.post('/:user', (req, res) =>
user = req.body;
console.log(req.body);
user['userScore'] = parseInt(user.userScore);
//si el texto es true, entonces la comparacion da un true en boolean si no pues false
user['first_login'] = (user.first_login === 'true');
user['isSelected'] = (user.isSelected === 'true');
return new Promise((resolve, reject) =>
return collection_userRef.doc(user.userId).set(user).then(() =>
console.log("USER ID: " + user.userId);
res.send(user);
resolve(user);
return user;
).catch(error =>
reject(error);
);
);
);
function deleteUser()
app.delete('/:id', (req, res) =>
let idToDelete = req.params.id;
console.log("USER DELETED: " + idToDelete);
let message = "message": "Usuario borrado!";
return new Promise((resolve, reject) =>
return collection_userRef.doc(idToDelete).delete().then(() =>
console.log(message);
resolve(message);
res.send(message);
return message;
).catch(error =>
console.log(error);
reject(error);
res.send(error.toString());
);
// return collection_userRef.where("userEmail", "==", email).get().then(querySnapshot =>
//
// querySnapshot.forEach(doc =>
//
// console.log("doc info: " + doc.get("userId"));
//
// user =
// "userName": doc.get("userName"),
// "userId": doc.get("userId"),
// "userScore": doc.get("userScore"),
// "first_login": doc.get("first_login"),
// "section": doc.get("section"),
// "userEmail": doc.get("userEmail")
// ;
//
// doc.ref.delete();
// );
//
//
// resolve(message);
// res.send(message);
// return message;
// ).catch(error =>
// console.log(error.message);
// reject(error);
// );
);
);
function getUserByEmail()
app.get('/:email', (req, res) =>
let user_email = req.params.email;
console.log("USUARIO EMAIL: " + req.params.email);
return new Promise((resolve, reject) =>
return collection_userRef.where("userEmail", "==", user_email).get().then(snapshot =>
snapshot.forEach(doc =>
console.log("doc info: " + doc.get("userEmail"));
user =
"userName": doc.get("userName"),
"userId": doc.get("userId"),
"userScore": doc.get("userScore"),
"first_login": doc.get("first_login"),
"section": doc.get("section"),
"userEmail": doc.get("userEmail")
;
);
//console.log("USUARIO: " + user.userName);
resolve(user);
return res.send(user);
).catch(err =>
reject(err);
console.log(err.toString());
);
);
);
function getKeyFileName()
return "./coprojedb_sdkfirebase_admin_key.json";
function getProyectId()
return "coprojedb";
https://github.com/sergio-27/coproje_v2/blob/admin_view/src/main/java/ruiz/coproje/entities/entities_dao_imp/UserImpDAO.java
It would be great if someone can explain me how can i create a crud with node js using firestore as database and from android requesting to the functions located at index.js. I mean i have created some functions like get, get passing parameters, deleted user and post it. So i have and idea about how make it work but i don have clear how organize the name of the url (/myapi/deleteuser/object)using express. Another question is, have i to use express()(or also named app) or router()(named router) to deploy crud functions with firestore? I am a bit lost... :(
node.js express google-cloud-firestore google-cloud-functions
add a comment |
Well I have my functions that query to firestore in nodejs and i am using express.
So i have a function that returns a user by the email provided from android.
Function in android that pass the email to nodejs
Function in nodejs that query to firestore to get user by email
I use this function to log in the user on my app and its works fine.
But the problem comes when to delete a user i use too the function getUserByEmail to get the user that i have to delete. I do not understand why but when i request the function getUserByEmail on the delete function it is returning the same value as if i were logging again. I do not know if i have to route my functions on nodejs or disable the cache.
Whatever it is i do not know how to do it and if you can explain me or paste a link about best practices to set up express in js and google cloud functions it would be a great help.
This is how i initialize the app express on my indexjs Despite having disable cache it is not working. Thanks!
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const Firestore = require('@google-cloud/firestore');
//const service_account = require(getKeyFileName());
const express = require('express');
const body_parser = require('body-parser');
const cors = require('cors');
const router = express.Router();
let app = express();
app.use(cors(origin: true));
app.use(body_parser.json());
app.use(body_parser.urlencoded( extended: true ));
app.use((req, res, next) =>
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next();
);
const firestore = new Firestore(
projectId: getProyectId(),
keyFilename: getKeyFileName()
);
const collection_userRef = firestore.collection('users/');
let user_model =
userName: "",
userId: "",
userScore: 0,
first_login: true,
section: "",
userEmail: ""
;
let user = null;
main();
// admin.initializeApp(
// credential: admin.credential.cert(service_account)
// );
function main()
admin.initializeApp(functions.config().firebase);
userDao();
function userDao()
getUserByEmail();
exports.getUserByEmail = functions.https.onRequest(app);
postUser();
exports.postUser = functions.https.onRequest(app);
getUsers();
exports.getUsers = functions.https.onRequest(app);
deleteUser();
exports.deleteUser = functions.https.onRequest(app);
function getUsers()
app.get('', (req, res) =>
const users = ;
return new Promise((resolve, reject) =>
return collection_userRef.get().then(snapshot =>
snapshot.forEach(doc =>
console.log(doc.data());
users.push(doc.data());
);
resolve(users);
res.send(users);
return users;
).catch(error =>
console.log(error.message);
reject(error);
);
);
);
//using post to put user and to update it
function postUser()
app.post('/:user', (req, res) =>
user = req.body;
console.log(req.body);
user['userScore'] = parseInt(user.userScore);
//si el texto es true, entonces la comparacion da un true en boolean si no pues false
user['first_login'] = (user.first_login === 'true');
user['isSelected'] = (user.isSelected === 'true');
return new Promise((resolve, reject) =>
return collection_userRef.doc(user.userId).set(user).then(() =>
console.log("USER ID: " + user.userId);
res.send(user);
resolve(user);
return user;
).catch(error =>
reject(error);
);
);
);
function deleteUser()
app.delete('/:id', (req, res) =>
let idToDelete = req.params.id;
console.log("USER DELETED: " + idToDelete);
let message = "message": "Usuario borrado!";
return new Promise((resolve, reject) =>
return collection_userRef.doc(idToDelete).delete().then(() =>
console.log(message);
resolve(message);
res.send(message);
return message;
).catch(error =>
console.log(error);
reject(error);
res.send(error.toString());
);
// return collection_userRef.where("userEmail", "==", email).get().then(querySnapshot =>
//
// querySnapshot.forEach(doc =>
//
// console.log("doc info: " + doc.get("userId"));
//
// user =
// "userName": doc.get("userName"),
// "userId": doc.get("userId"),
// "userScore": doc.get("userScore"),
// "first_login": doc.get("first_login"),
// "section": doc.get("section"),
// "userEmail": doc.get("userEmail")
// ;
//
// doc.ref.delete();
// );
//
//
// resolve(message);
// res.send(message);
// return message;
// ).catch(error =>
// console.log(error.message);
// reject(error);
// );
);
);
function getUserByEmail()
app.get('/:email', (req, res) =>
let user_email = req.params.email;
console.log("USUARIO EMAIL: " + req.params.email);
return new Promise((resolve, reject) =>
return collection_userRef.where("userEmail", "==", user_email).get().then(snapshot =>
snapshot.forEach(doc =>
console.log("doc info: " + doc.get("userEmail"));
user =
"userName": doc.get("userName"),
"userId": doc.get("userId"),
"userScore": doc.get("userScore"),
"first_login": doc.get("first_login"),
"section": doc.get("section"),
"userEmail": doc.get("userEmail")
;
);
//console.log("USUARIO: " + user.userName);
resolve(user);
return res.send(user);
).catch(err =>
reject(err);
console.log(err.toString());
);
);
);
function getKeyFileName()
return "./coprojedb_sdkfirebase_admin_key.json";
function getProyectId()
return "coprojedb";
https://github.com/sergio-27/coproje_v2/blob/admin_view/src/main/java/ruiz/coproje/entities/entities_dao_imp/UserImpDAO.java
It would be great if someone can explain me how can i create a crud with node js using firestore as database and from android requesting to the functions located at index.js. I mean i have created some functions like get, get passing parameters, deleted user and post it. So i have and idea about how make it work but i don have clear how organize the name of the url (/myapi/deleteuser/object)using express. Another question is, have i to use express()(or also named app) or router()(named router) to deploy crud functions with firestore? I am a bit lost... :(
node.js express google-cloud-firestore google-cloud-functions
If you intend to reference code on Stack Overflow, please copy it into the question instead of linking to it. The question should contain everything needed in order to answer the question.
– Doug Stevenson
Nov 13 '18 at 20:59
@DougStevenson here you have! You are right this is better.
– Sergio
Nov 13 '18 at 23:00
add a comment |
Well I have my functions that query to firestore in nodejs and i am using express.
So i have a function that returns a user by the email provided from android.
Function in android that pass the email to nodejs
Function in nodejs that query to firestore to get user by email
I use this function to log in the user on my app and its works fine.
But the problem comes when to delete a user i use too the function getUserByEmail to get the user that i have to delete. I do not understand why but when i request the function getUserByEmail on the delete function it is returning the same value as if i were logging again. I do not know if i have to route my functions on nodejs or disable the cache.
Whatever it is i do not know how to do it and if you can explain me or paste a link about best practices to set up express in js and google cloud functions it would be a great help.
This is how i initialize the app express on my indexjs Despite having disable cache it is not working. Thanks!
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const Firestore = require('@google-cloud/firestore');
//const service_account = require(getKeyFileName());
const express = require('express');
const body_parser = require('body-parser');
const cors = require('cors');
const router = express.Router();
let app = express();
app.use(cors(origin: true));
app.use(body_parser.json());
app.use(body_parser.urlencoded( extended: true ));
app.use((req, res, next) =>
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next();
);
const firestore = new Firestore(
projectId: getProyectId(),
keyFilename: getKeyFileName()
);
const collection_userRef = firestore.collection('users/');
let user_model =
userName: "",
userId: "",
userScore: 0,
first_login: true,
section: "",
userEmail: ""
;
let user = null;
main();
// admin.initializeApp(
// credential: admin.credential.cert(service_account)
// );
function main()
admin.initializeApp(functions.config().firebase);
userDao();
function userDao()
getUserByEmail();
exports.getUserByEmail = functions.https.onRequest(app);
postUser();
exports.postUser = functions.https.onRequest(app);
getUsers();
exports.getUsers = functions.https.onRequest(app);
deleteUser();
exports.deleteUser = functions.https.onRequest(app);
function getUsers()
app.get('', (req, res) =>
const users = ;
return new Promise((resolve, reject) =>
return collection_userRef.get().then(snapshot =>
snapshot.forEach(doc =>
console.log(doc.data());
users.push(doc.data());
);
resolve(users);
res.send(users);
return users;
).catch(error =>
console.log(error.message);
reject(error);
);
);
);
//using post to put user and to update it
function postUser()
app.post('/:user', (req, res) =>
user = req.body;
console.log(req.body);
user['userScore'] = parseInt(user.userScore);
//si el texto es true, entonces la comparacion da un true en boolean si no pues false
user['first_login'] = (user.first_login === 'true');
user['isSelected'] = (user.isSelected === 'true');
return new Promise((resolve, reject) =>
return collection_userRef.doc(user.userId).set(user).then(() =>
console.log("USER ID: " + user.userId);
res.send(user);
resolve(user);
return user;
).catch(error =>
reject(error);
);
);
);
function deleteUser()
app.delete('/:id', (req, res) =>
let idToDelete = req.params.id;
console.log("USER DELETED: " + idToDelete);
let message = "message": "Usuario borrado!";
return new Promise((resolve, reject) =>
return collection_userRef.doc(idToDelete).delete().then(() =>
console.log(message);
resolve(message);
res.send(message);
return message;
).catch(error =>
console.log(error);
reject(error);
res.send(error.toString());
);
// return collection_userRef.where("userEmail", "==", email).get().then(querySnapshot =>
//
// querySnapshot.forEach(doc =>
//
// console.log("doc info: " + doc.get("userId"));
//
// user =
// "userName": doc.get("userName"),
// "userId": doc.get("userId"),
// "userScore": doc.get("userScore"),
// "first_login": doc.get("first_login"),
// "section": doc.get("section"),
// "userEmail": doc.get("userEmail")
// ;
//
// doc.ref.delete();
// );
//
//
// resolve(message);
// res.send(message);
// return message;
// ).catch(error =>
// console.log(error.message);
// reject(error);
// );
);
);
function getUserByEmail()
app.get('/:email', (req, res) =>
let user_email = req.params.email;
console.log("USUARIO EMAIL: " + req.params.email);
return new Promise((resolve, reject) =>
return collection_userRef.where("userEmail", "==", user_email).get().then(snapshot =>
snapshot.forEach(doc =>
console.log("doc info: " + doc.get("userEmail"));
user =
"userName": doc.get("userName"),
"userId": doc.get("userId"),
"userScore": doc.get("userScore"),
"first_login": doc.get("first_login"),
"section": doc.get("section"),
"userEmail": doc.get("userEmail")
;
);
//console.log("USUARIO: " + user.userName);
resolve(user);
return res.send(user);
).catch(err =>
reject(err);
console.log(err.toString());
);
);
);
function getKeyFileName()
return "./coprojedb_sdkfirebase_admin_key.json";
function getProyectId()
return "coprojedb";
https://github.com/sergio-27/coproje_v2/blob/admin_view/src/main/java/ruiz/coproje/entities/entities_dao_imp/UserImpDAO.java
It would be great if someone can explain me how can i create a crud with node js using firestore as database and from android requesting to the functions located at index.js. I mean i have created some functions like get, get passing parameters, deleted user and post it. So i have and idea about how make it work but i don have clear how organize the name of the url (/myapi/deleteuser/object)using express. Another question is, have i to use express()(or also named app) or router()(named router) to deploy crud functions with firestore? I am a bit lost... :(
node.js express google-cloud-firestore google-cloud-functions
Well I have my functions that query to firestore in nodejs and i am using express.
So i have a function that returns a user by the email provided from android.
Function in android that pass the email to nodejs
Function in nodejs that query to firestore to get user by email
I use this function to log in the user on my app and its works fine.
But the problem comes when to delete a user i use too the function getUserByEmail to get the user that i have to delete. I do not understand why but when i request the function getUserByEmail on the delete function it is returning the same value as if i were logging again. I do not know if i have to route my functions on nodejs or disable the cache.
Whatever it is i do not know how to do it and if you can explain me or paste a link about best practices to set up express in js and google cloud functions it would be a great help.
This is how i initialize the app express on my indexjs Despite having disable cache it is not working. Thanks!
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const Firestore = require('@google-cloud/firestore');
//const service_account = require(getKeyFileName());
const express = require('express');
const body_parser = require('body-parser');
const cors = require('cors');
const router = express.Router();
let app = express();
app.use(cors(origin: true));
app.use(body_parser.json());
app.use(body_parser.urlencoded( extended: true ));
app.use((req, res, next) =>
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next();
);
const firestore = new Firestore(
projectId: getProyectId(),
keyFilename: getKeyFileName()
);
const collection_userRef = firestore.collection('users/');
let user_model =
userName: "",
userId: "",
userScore: 0,
first_login: true,
section: "",
userEmail: ""
;
let user = null;
main();
// admin.initializeApp(
// credential: admin.credential.cert(service_account)
// );
function main()
admin.initializeApp(functions.config().firebase);
userDao();
function userDao()
getUserByEmail();
exports.getUserByEmail = functions.https.onRequest(app);
postUser();
exports.postUser = functions.https.onRequest(app);
getUsers();
exports.getUsers = functions.https.onRequest(app);
deleteUser();
exports.deleteUser = functions.https.onRequest(app);
function getUsers()
app.get('', (req, res) =>
const users = ;
return new Promise((resolve, reject) =>
return collection_userRef.get().then(snapshot =>
snapshot.forEach(doc =>
console.log(doc.data());
users.push(doc.data());
);
resolve(users);
res.send(users);
return users;
).catch(error =>
console.log(error.message);
reject(error);
);
);
);
//using post to put user and to update it
function postUser()
app.post('/:user', (req, res) =>
user = req.body;
console.log(req.body);
user['userScore'] = parseInt(user.userScore);
//si el texto es true, entonces la comparacion da un true en boolean si no pues false
user['first_login'] = (user.first_login === 'true');
user['isSelected'] = (user.isSelected === 'true');
return new Promise((resolve, reject) =>
return collection_userRef.doc(user.userId).set(user).then(() =>
console.log("USER ID: " + user.userId);
res.send(user);
resolve(user);
return user;
).catch(error =>
reject(error);
);
);
);
function deleteUser()
app.delete('/:id', (req, res) =>
let idToDelete = req.params.id;
console.log("USER DELETED: " + idToDelete);
let message = "message": "Usuario borrado!";
return new Promise((resolve, reject) =>
return collection_userRef.doc(idToDelete).delete().then(() =>
console.log(message);
resolve(message);
res.send(message);
return message;
).catch(error =>
console.log(error);
reject(error);
res.send(error.toString());
);
// return collection_userRef.where("userEmail", "==", email).get().then(querySnapshot =>
//
// querySnapshot.forEach(doc =>
//
// console.log("doc info: " + doc.get("userId"));
//
// user =
// "userName": doc.get("userName"),
// "userId": doc.get("userId"),
// "userScore": doc.get("userScore"),
// "first_login": doc.get("first_login"),
// "section": doc.get("section"),
// "userEmail": doc.get("userEmail")
// ;
//
// doc.ref.delete();
// );
//
//
// resolve(message);
// res.send(message);
// return message;
// ).catch(error =>
// console.log(error.message);
// reject(error);
// );
);
);
function getUserByEmail()
app.get('/:email', (req, res) =>
let user_email = req.params.email;
console.log("USUARIO EMAIL: " + req.params.email);
return new Promise((resolve, reject) =>
return collection_userRef.where("userEmail", "==", user_email).get().then(snapshot =>
snapshot.forEach(doc =>
console.log("doc info: " + doc.get("userEmail"));
user =
"userName": doc.get("userName"),
"userId": doc.get("userId"),
"userScore": doc.get("userScore"),
"first_login": doc.get("first_login"),
"section": doc.get("section"),
"userEmail": doc.get("userEmail")
;
);
//console.log("USUARIO: " + user.userName);
resolve(user);
return res.send(user);
).catch(err =>
reject(err);
console.log(err.toString());
);
);
);
function getKeyFileName()
return "./coprojedb_sdkfirebase_admin_key.json";
function getProyectId()
return "coprojedb";
https://github.com/sergio-27/coproje_v2/blob/admin_view/src/main/java/ruiz/coproje/entities/entities_dao_imp/UserImpDAO.java
It would be great if someone can explain me how can i create a crud with node js using firestore as database and from android requesting to the functions located at index.js. I mean i have created some functions like get, get passing parameters, deleted user and post it. So i have and idea about how make it work but i don have clear how organize the name of the url (/myapi/deleteuser/object)using express. Another question is, have i to use express()(or also named app) or router()(named router) to deploy crud functions with firestore? I am a bit lost... :(
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const Firestore = require('@google-cloud/firestore');
//const service_account = require(getKeyFileName());
const express = require('express');
const body_parser = require('body-parser');
const cors = require('cors');
const router = express.Router();
let app = express();
app.use(cors(origin: true));
app.use(body_parser.json());
app.use(body_parser.urlencoded( extended: true ));
app.use((req, res, next) =>
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next();
);
const firestore = new Firestore(
projectId: getProyectId(),
keyFilename: getKeyFileName()
);
const collection_userRef = firestore.collection('users/');
let user_model =
userName: "",
userId: "",
userScore: 0,
first_login: true,
section: "",
userEmail: ""
;
let user = null;
main();
// admin.initializeApp(
// credential: admin.credential.cert(service_account)
// );
function main()
admin.initializeApp(functions.config().firebase);
userDao();
function userDao()
getUserByEmail();
exports.getUserByEmail = functions.https.onRequest(app);
postUser();
exports.postUser = functions.https.onRequest(app);
getUsers();
exports.getUsers = functions.https.onRequest(app);
deleteUser();
exports.deleteUser = functions.https.onRequest(app);
function getUsers()
app.get('', (req, res) =>
const users = ;
return new Promise((resolve, reject) =>
return collection_userRef.get().then(snapshot =>
snapshot.forEach(doc =>
console.log(doc.data());
users.push(doc.data());
);
resolve(users);
res.send(users);
return users;
).catch(error =>
console.log(error.message);
reject(error);
);
);
);
//using post to put user and to update it
function postUser()
app.post('/:user', (req, res) =>
user = req.body;
console.log(req.body);
user['userScore'] = parseInt(user.userScore);
//si el texto es true, entonces la comparacion da un true en boolean si no pues false
user['first_login'] = (user.first_login === 'true');
user['isSelected'] = (user.isSelected === 'true');
return new Promise((resolve, reject) =>
return collection_userRef.doc(user.userId).set(user).then(() =>
console.log("USER ID: " + user.userId);
res.send(user);
resolve(user);
return user;
).catch(error =>
reject(error);
);
);
);
function deleteUser()
app.delete('/:id', (req, res) =>
let idToDelete = req.params.id;
console.log("USER DELETED: " + idToDelete);
let message = "message": "Usuario borrado!";
return new Promise((resolve, reject) =>
return collection_userRef.doc(idToDelete).delete().then(() =>
console.log(message);
resolve(message);
res.send(message);
return message;
).catch(error =>
console.log(error);
reject(error);
res.send(error.toString());
);
// return collection_userRef.where("userEmail", "==", email).get().then(querySnapshot =>
//
// querySnapshot.forEach(doc =>
//
// console.log("doc info: " + doc.get("userId"));
//
// user =
// "userName": doc.get("userName"),
// "userId": doc.get("userId"),
// "userScore": doc.get("userScore"),
// "first_login": doc.get("first_login"),
// "section": doc.get("section"),
// "userEmail": doc.get("userEmail")
// ;
//
// doc.ref.delete();
// );
//
//
// resolve(message);
// res.send(message);
// return message;
// ).catch(error =>
// console.log(error.message);
// reject(error);
// );
);
);
function getUserByEmail()
app.get('/:email', (req, res) =>
let user_email = req.params.email;
console.log("USUARIO EMAIL: " + req.params.email);
return new Promise((resolve, reject) =>
return collection_userRef.where("userEmail", "==", user_email).get().then(snapshot =>
snapshot.forEach(doc =>
console.log("doc info: " + doc.get("userEmail"));
user =
"userName": doc.get("userName"),
"userId": doc.get("userId"),
"userScore": doc.get("userScore"),
"first_login": doc.get("first_login"),
"section": doc.get("section"),
"userEmail": doc.get("userEmail")
;
);
//console.log("USUARIO: " + user.userName);
resolve(user);
return res.send(user);
).catch(err =>
reject(err);
console.log(err.toString());
);
);
);
function getKeyFileName()
return "./coprojedb_sdkfirebase_admin_key.json";
function getProyectId()
return "coprojedb";
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const Firestore = require('@google-cloud/firestore');
//const service_account = require(getKeyFileName());
const express = require('express');
const body_parser = require('body-parser');
const cors = require('cors');
const router = express.Router();
let app = express();
app.use(cors(origin: true));
app.use(body_parser.json());
app.use(body_parser.urlencoded( extended: true ));
app.use((req, res, next) =>
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next();
);
const firestore = new Firestore(
projectId: getProyectId(),
keyFilename: getKeyFileName()
);
const collection_userRef = firestore.collection('users/');
let user_model =
userName: "",
userId: "",
userScore: 0,
first_login: true,
section: "",
userEmail: ""
;
let user = null;
main();
// admin.initializeApp(
// credential: admin.credential.cert(service_account)
// );
function main()
admin.initializeApp(functions.config().firebase);
userDao();
function userDao()
getUserByEmail();
exports.getUserByEmail = functions.https.onRequest(app);
postUser();
exports.postUser = functions.https.onRequest(app);
getUsers();
exports.getUsers = functions.https.onRequest(app);
deleteUser();
exports.deleteUser = functions.https.onRequest(app);
function getUsers()
app.get('', (req, res) =>
const users = ;
return new Promise((resolve, reject) =>
return collection_userRef.get().then(snapshot =>
snapshot.forEach(doc =>
console.log(doc.data());
users.push(doc.data());
);
resolve(users);
res.send(users);
return users;
).catch(error =>
console.log(error.message);
reject(error);
);
);
);
//using post to put user and to update it
function postUser()
app.post('/:user', (req, res) =>
user = req.body;
console.log(req.body);
user['userScore'] = parseInt(user.userScore);
//si el texto es true, entonces la comparacion da un true en boolean si no pues false
user['first_login'] = (user.first_login === 'true');
user['isSelected'] = (user.isSelected === 'true');
return new Promise((resolve, reject) =>
return collection_userRef.doc(user.userId).set(user).then(() =>
console.log("USER ID: " + user.userId);
res.send(user);
resolve(user);
return user;
).catch(error =>
reject(error);
);
);
);
function deleteUser()
app.delete('/:id', (req, res) =>
let idToDelete = req.params.id;
console.log("USER DELETED: " + idToDelete);
let message = "message": "Usuario borrado!";
return new Promise((resolve, reject) =>
return collection_userRef.doc(idToDelete).delete().then(() =>
console.log(message);
resolve(message);
res.send(message);
return message;
).catch(error =>
console.log(error);
reject(error);
res.send(error.toString());
);
// return collection_userRef.where("userEmail", "==", email).get().then(querySnapshot =>
//
// querySnapshot.forEach(doc =>
//
// console.log("doc info: " + doc.get("userId"));
//
// user =
// "userName": doc.get("userName"),
// "userId": doc.get("userId"),
// "userScore": doc.get("userScore"),
// "first_login": doc.get("first_login"),
// "section": doc.get("section"),
// "userEmail": doc.get("userEmail")
// ;
//
// doc.ref.delete();
// );
//
//
// resolve(message);
// res.send(message);
// return message;
// ).catch(error =>
// console.log(error.message);
// reject(error);
// );
);
);
function getUserByEmail()
app.get('/:email', (req, res) =>
let user_email = req.params.email;
console.log("USUARIO EMAIL: " + req.params.email);
return new Promise((resolve, reject) =>
return collection_userRef.where("userEmail", "==", user_email).get().then(snapshot =>
snapshot.forEach(doc =>
console.log("doc info: " + doc.get("userEmail"));
user =
"userName": doc.get("userName"),
"userId": doc.get("userId"),
"userScore": doc.get("userScore"),
"first_login": doc.get("first_login"),
"section": doc.get("section"),
"userEmail": doc.get("userEmail")
;
);
//console.log("USUARIO: " + user.userName);
resolve(user);
return res.send(user);
).catch(err =>
reject(err);
console.log(err.toString());
);
);
);
function getKeyFileName()
return "./coprojedb_sdkfirebase_admin_key.json";
function getProyectId()
return "coprojedb";
node.js express google-cloud-firestore google-cloud-functions
node.js express google-cloud-firestore google-cloud-functions
edited Nov 13 '18 at 23:20
Sergio
asked Nov 13 '18 at 20:57
SergioSergio
53
53
If you intend to reference code on Stack Overflow, please copy it into the question instead of linking to it. The question should contain everything needed in order to answer the question.
– Doug Stevenson
Nov 13 '18 at 20:59
@DougStevenson here you have! You are right this is better.
– Sergio
Nov 13 '18 at 23:00
add a comment |
If you intend to reference code on Stack Overflow, please copy it into the question instead of linking to it. The question should contain everything needed in order to answer the question.
– Doug Stevenson
Nov 13 '18 at 20:59
@DougStevenson here you have! You are right this is better.
– Sergio
Nov 13 '18 at 23:00
If you intend to reference code on Stack Overflow, please copy it into the question instead of linking to it. The question should contain everything needed in order to answer the question.
– Doug Stevenson
Nov 13 '18 at 20:59
If you intend to reference code on Stack Overflow, please copy it into the question instead of linking to it. The question should contain everything needed in order to answer the question.
– Doug Stevenson
Nov 13 '18 at 20:59
@DougStevenson here you have! You are right this is better.
– Sergio
Nov 13 '18 at 23:00
@DougStevenson here you have! You are right this is better.
– Sergio
Nov 13 '18 at 23:00
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289396%2fexpress-in-nodejs-is-keeping-cache-with-cloud-functions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289396%2fexpress-in-nodejs-is-keeping-cache-with-cloud-functions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you intend to reference code on Stack Overflow, please copy it into the question instead of linking to it. The question should contain everything needed in order to answer the question.
– Doug Stevenson
Nov 13 '18 at 20:59
@DougStevenson here you have! You are right this is better.
– Sergio
Nov 13 '18 at 23:00