How to loop “pushing” to Firebase database using cloud functions










-1















I need to add to my database a list of 3 students to 1 teacher (and assume it doesn't exist yet). Also should return dictionary to the client. I'm using cloud functions. Here is my idea, my app is frozen when I run it.



exports.addNewStudents = functions.https.onCall((data, context) => {
const totalStudent = data.total; //"3"
const teacher = data.teacher //"Robert"
var studentListToReturn = new Dictionary<string, object>();

for (int i=0; i<totalStudent,i++ )
{ // 'i' is going to be the student's ID and I use it in the path:
return admin.database().ref('studentsTable/teacher/'+i).push(

date: Date();,
class: "Highschool",
).then(() =>
studentListToReturn[i]=i ;
)

return studentListToReturn;


It should look like this on my DB:




  • studentsTable




    • Robert



      • 0 date: 11/13/2018 , class:"Highschool"

      • 1 date: 11/13/2018 , class:"Highschool"

      • 2 date: 11/13/2018 , class:"Highschool"



I'm new to cloud functions (and js) can you please help?










share|improve this question
























  • This is illegal syntax: new Dictionary<string, object>(); If you're new to JavaScript, Cloud Functions is not the best way to learn it. I recommend first reading the Firebase documentation and/or taking the Firebase codelab. They cover many basic JavaScript, Web and Firebase interactions. You could also use the Admin SDK in a local Node.js process, which can be debugged with a local debugger. After those you'll be much better equipped to write code for Cloud Functions too.

    – Frank van Puffelen
    Nov 14 '18 at 4:27











  • Thanks, I'm not that new... I usually program in C++/ Java, and I know js basics, it's not that different for what I need. My question is about the loop and that specific function call to the server and the example I need. Thtat's why I didn't add the node.js tag that I see now that you added

    – SHAI
    Nov 14 '18 at 5:51
















-1















I need to add to my database a list of 3 students to 1 teacher (and assume it doesn't exist yet). Also should return dictionary to the client. I'm using cloud functions. Here is my idea, my app is frozen when I run it.



exports.addNewStudents = functions.https.onCall((data, context) => {
const totalStudent = data.total; //"3"
const teacher = data.teacher //"Robert"
var studentListToReturn = new Dictionary<string, object>();

for (int i=0; i<totalStudent,i++ )
{ // 'i' is going to be the student's ID and I use it in the path:
return admin.database().ref('studentsTable/teacher/'+i).push(

date: Date();,
class: "Highschool",
).then(() =>
studentListToReturn[i]=i ;
)

return studentListToReturn;


It should look like this on my DB:




  • studentsTable




    • Robert



      • 0 date: 11/13/2018 , class:"Highschool"

      • 1 date: 11/13/2018 , class:"Highschool"

      • 2 date: 11/13/2018 , class:"Highschool"



I'm new to cloud functions (and js) can you please help?










share|improve this question
























  • This is illegal syntax: new Dictionary<string, object>(); If you're new to JavaScript, Cloud Functions is not the best way to learn it. I recommend first reading the Firebase documentation and/or taking the Firebase codelab. They cover many basic JavaScript, Web and Firebase interactions. You could also use the Admin SDK in a local Node.js process, which can be debugged with a local debugger. After those you'll be much better equipped to write code for Cloud Functions too.

    – Frank van Puffelen
    Nov 14 '18 at 4:27











  • Thanks, I'm not that new... I usually program in C++/ Java, and I know js basics, it's not that different for what I need. My question is about the loop and that specific function call to the server and the example I need. Thtat's why I didn't add the node.js tag that I see now that you added

    – SHAI
    Nov 14 '18 at 5:51














-1












-1








-1








I need to add to my database a list of 3 students to 1 teacher (and assume it doesn't exist yet). Also should return dictionary to the client. I'm using cloud functions. Here is my idea, my app is frozen when I run it.



exports.addNewStudents = functions.https.onCall((data, context) => {
const totalStudent = data.total; //"3"
const teacher = data.teacher //"Robert"
var studentListToReturn = new Dictionary<string, object>();

for (int i=0; i<totalStudent,i++ )
{ // 'i' is going to be the student's ID and I use it in the path:
return admin.database().ref('studentsTable/teacher/'+i).push(

date: Date();,
class: "Highschool",
).then(() =>
studentListToReturn[i]=i ;
)

return studentListToReturn;


It should look like this on my DB:




  • studentsTable




    • Robert



      • 0 date: 11/13/2018 , class:"Highschool"

      • 1 date: 11/13/2018 , class:"Highschool"

      • 2 date: 11/13/2018 , class:"Highschool"



I'm new to cloud functions (and js) can you please help?










share|improve this question
















I need to add to my database a list of 3 students to 1 teacher (and assume it doesn't exist yet). Also should return dictionary to the client. I'm using cloud functions. Here is my idea, my app is frozen when I run it.



exports.addNewStudents = functions.https.onCall((data, context) => {
const totalStudent = data.total; //"3"
const teacher = data.teacher //"Robert"
var studentListToReturn = new Dictionary<string, object>();

for (int i=0; i<totalStudent,i++ )
{ // 'i' is going to be the student's ID and I use it in the path:
return admin.database().ref('studentsTable/teacher/'+i).push(

date: Date();,
class: "Highschool",
).then(() =>
studentListToReturn[i]=i ;
)

return studentListToReturn;


It should look like this on my DB:




  • studentsTable




    • Robert



      • 0 date: 11/13/2018 , class:"Highschool"

      • 1 date: 11/13/2018 , class:"Highschool"

      • 2 date: 11/13/2018 , class:"Highschool"



I'm new to cloud functions (and js) can you please help?







node.js firebase firebase-realtime-database google-cloud-functions






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 4:23









Frank van Puffelen

234k29380407




234k29380407










asked Nov 14 '18 at 4:17









SHAISHAI

164112




164112












  • This is illegal syntax: new Dictionary<string, object>(); If you're new to JavaScript, Cloud Functions is not the best way to learn it. I recommend first reading the Firebase documentation and/or taking the Firebase codelab. They cover many basic JavaScript, Web and Firebase interactions. You could also use the Admin SDK in a local Node.js process, which can be debugged with a local debugger. After those you'll be much better equipped to write code for Cloud Functions too.

    – Frank van Puffelen
    Nov 14 '18 at 4:27











  • Thanks, I'm not that new... I usually program in C++/ Java, and I know js basics, it's not that different for what I need. My question is about the loop and that specific function call to the server and the example I need. Thtat's why I didn't add the node.js tag that I see now that you added

    – SHAI
    Nov 14 '18 at 5:51


















  • This is illegal syntax: new Dictionary<string, object>(); If you're new to JavaScript, Cloud Functions is not the best way to learn it. I recommend first reading the Firebase documentation and/or taking the Firebase codelab. They cover many basic JavaScript, Web and Firebase interactions. You could also use the Admin SDK in a local Node.js process, which can be debugged with a local debugger. After those you'll be much better equipped to write code for Cloud Functions too.

    – Frank van Puffelen
    Nov 14 '18 at 4:27











  • Thanks, I'm not that new... I usually program in C++/ Java, and I know js basics, it's not that different for what I need. My question is about the loop and that specific function call to the server and the example I need. Thtat's why I didn't add the node.js tag that I see now that you added

    – SHAI
    Nov 14 '18 at 5:51

















This is illegal syntax: new Dictionary<string, object>(); If you're new to JavaScript, Cloud Functions is not the best way to learn it. I recommend first reading the Firebase documentation and/or taking the Firebase codelab. They cover many basic JavaScript, Web and Firebase interactions. You could also use the Admin SDK in a local Node.js process, which can be debugged with a local debugger. After those you'll be much better equipped to write code for Cloud Functions too.

– Frank van Puffelen
Nov 14 '18 at 4:27





This is illegal syntax: new Dictionary<string, object>(); If you're new to JavaScript, Cloud Functions is not the best way to learn it. I recommend first reading the Firebase documentation and/or taking the Firebase codelab. They cover many basic JavaScript, Web and Firebase interactions. You could also use the Admin SDK in a local Node.js process, which can be debugged with a local debugger. After those you'll be much better equipped to write code for Cloud Functions too.

– Frank van Puffelen
Nov 14 '18 at 4:27













Thanks, I'm not that new... I usually program in C++/ Java, and I know js basics, it's not that different for what I need. My question is about the loop and that specific function call to the server and the example I need. Thtat's why I didn't add the node.js tag that I see now that you added

– SHAI
Nov 14 '18 at 5:51






Thanks, I'm not that new... I usually program in C++/ Java, and I know js basics, it's not that different for what I need. My question is about the loop and that specific function call to the server and the example I need. Thtat's why I didn't add the node.js tag that I see now that you added

– SHAI
Nov 14 '18 at 5:51













1 Answer
1






active

oldest

votes


















2














I have edited the code and made use of async await to make it more readable. There were a lot of JS syntax issues. I would suggest using the typescript firebase functions template and use visual studio code as an editor which will give you the IntelliSense. Also you would need to add in an auth check at the top to ensure only certain people can call the function.



exports.addNewStudents = functions.https.onCall(async (data, context) => 
// Need to add in an auth check to make sure they have permission to do the below

const totalStudent = data.total; //"3"
const teacher = data.teacher //"Robert"
const studentListToReturn: Array<any> = ;

for (let i = 0; i < totalStudent; i++ ) // 'i' is going to be the student's ID and I use it in the path:
const student: any =
studentId: i,
date: Date(),
class: "Highschool"

await admin.database().ref(`studentsTable/$teacher/$i`).set(student)
studentListToReturn.push(student);

return studentListToReturn;
);





share|improve this answer























  • Thanks for auth check- is it enough to check 'context.auth!=null' ? or do I need to connect to my database and check if context.auth.uid exists theree?

    – SHAI
    Nov 14 '18 at 19:42











  • It really depends if you want all authenticated users to be able to do this operation. If not then you'd want to check your database for a list of allowed user ids or something. Many ways to achieve this just about your needs

    – Jack Woodward
    Nov 15 '18 at 9:21










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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293131%2fhow-to-loop-pushing-to-firebase-database-using-cloud-functions%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














I have edited the code and made use of async await to make it more readable. There were a lot of JS syntax issues. I would suggest using the typescript firebase functions template and use visual studio code as an editor which will give you the IntelliSense. Also you would need to add in an auth check at the top to ensure only certain people can call the function.



exports.addNewStudents = functions.https.onCall(async (data, context) => 
// Need to add in an auth check to make sure they have permission to do the below

const totalStudent = data.total; //"3"
const teacher = data.teacher //"Robert"
const studentListToReturn: Array<any> = ;

for (let i = 0; i < totalStudent; i++ ) // 'i' is going to be the student's ID and I use it in the path:
const student: any =
studentId: i,
date: Date(),
class: "Highschool"

await admin.database().ref(`studentsTable/$teacher/$i`).set(student)
studentListToReturn.push(student);

return studentListToReturn;
);





share|improve this answer























  • Thanks for auth check- is it enough to check 'context.auth!=null' ? or do I need to connect to my database and check if context.auth.uid exists theree?

    – SHAI
    Nov 14 '18 at 19:42











  • It really depends if you want all authenticated users to be able to do this operation. If not then you'd want to check your database for a list of allowed user ids or something. Many ways to achieve this just about your needs

    – Jack Woodward
    Nov 15 '18 at 9:21















2














I have edited the code and made use of async await to make it more readable. There were a lot of JS syntax issues. I would suggest using the typescript firebase functions template and use visual studio code as an editor which will give you the IntelliSense. Also you would need to add in an auth check at the top to ensure only certain people can call the function.



exports.addNewStudents = functions.https.onCall(async (data, context) => 
// Need to add in an auth check to make sure they have permission to do the below

const totalStudent = data.total; //"3"
const teacher = data.teacher //"Robert"
const studentListToReturn: Array<any> = ;

for (let i = 0; i < totalStudent; i++ ) // 'i' is going to be the student's ID and I use it in the path:
const student: any =
studentId: i,
date: Date(),
class: "Highschool"

await admin.database().ref(`studentsTable/$teacher/$i`).set(student)
studentListToReturn.push(student);

return studentListToReturn;
);





share|improve this answer























  • Thanks for auth check- is it enough to check 'context.auth!=null' ? or do I need to connect to my database and check if context.auth.uid exists theree?

    – SHAI
    Nov 14 '18 at 19:42











  • It really depends if you want all authenticated users to be able to do this operation. If not then you'd want to check your database for a list of allowed user ids or something. Many ways to achieve this just about your needs

    – Jack Woodward
    Nov 15 '18 at 9:21













2












2








2







I have edited the code and made use of async await to make it more readable. There were a lot of JS syntax issues. I would suggest using the typescript firebase functions template and use visual studio code as an editor which will give you the IntelliSense. Also you would need to add in an auth check at the top to ensure only certain people can call the function.



exports.addNewStudents = functions.https.onCall(async (data, context) => 
// Need to add in an auth check to make sure they have permission to do the below

const totalStudent = data.total; //"3"
const teacher = data.teacher //"Robert"
const studentListToReturn: Array<any> = ;

for (let i = 0; i < totalStudent; i++ ) // 'i' is going to be the student's ID and I use it in the path:
const student: any =
studentId: i,
date: Date(),
class: "Highschool"

await admin.database().ref(`studentsTable/$teacher/$i`).set(student)
studentListToReturn.push(student);

return studentListToReturn;
);





share|improve this answer













I have edited the code and made use of async await to make it more readable. There were a lot of JS syntax issues. I would suggest using the typescript firebase functions template and use visual studio code as an editor which will give you the IntelliSense. Also you would need to add in an auth check at the top to ensure only certain people can call the function.



exports.addNewStudents = functions.https.onCall(async (data, context) => 
// Need to add in an auth check to make sure they have permission to do the below

const totalStudent = data.total; //"3"
const teacher = data.teacher //"Robert"
const studentListToReturn: Array<any> = ;

for (let i = 0; i < totalStudent; i++ ) // 'i' is going to be the student's ID and I use it in the path:
const student: any =
studentId: i,
date: Date(),
class: "Highschool"

await admin.database().ref(`studentsTable/$teacher/$i`).set(student)
studentListToReturn.push(student);

return studentListToReturn;
);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 10:17









Jack WoodwardJack Woodward

62149




62149












  • Thanks for auth check- is it enough to check 'context.auth!=null' ? or do I need to connect to my database and check if context.auth.uid exists theree?

    – SHAI
    Nov 14 '18 at 19:42











  • It really depends if you want all authenticated users to be able to do this operation. If not then you'd want to check your database for a list of allowed user ids or something. Many ways to achieve this just about your needs

    – Jack Woodward
    Nov 15 '18 at 9:21

















  • Thanks for auth check- is it enough to check 'context.auth!=null' ? or do I need to connect to my database and check if context.auth.uid exists theree?

    – SHAI
    Nov 14 '18 at 19:42











  • It really depends if you want all authenticated users to be able to do this operation. If not then you'd want to check your database for a list of allowed user ids or something. Many ways to achieve this just about your needs

    – Jack Woodward
    Nov 15 '18 at 9:21
















Thanks for auth check- is it enough to check 'context.auth!=null' ? or do I need to connect to my database and check if context.auth.uid exists theree?

– SHAI
Nov 14 '18 at 19:42





Thanks for auth check- is it enough to check 'context.auth!=null' ? or do I need to connect to my database and check if context.auth.uid exists theree?

– SHAI
Nov 14 '18 at 19:42













It really depends if you want all authenticated users to be able to do this operation. If not then you'd want to check your database for a list of allowed user ids or something. Many ways to achieve this just about your needs

– Jack Woodward
Nov 15 '18 at 9:21





It really depends if you want all authenticated users to be able to do this operation. If not then you'd want to check your database for a list of allowed user ids or something. Many ways to achieve this just about your needs

– Jack Woodward
Nov 15 '18 at 9:21

















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293131%2fhow-to-loop-pushing-to-firebase-database-using-cloud-functions%23new-answer', 'question_page');

);

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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto