How to add data to Cloud Firestore?

烈酒焚心 提交于 2020-02-26 03:20:06

问题


The problem is that data is not inserted.

Here is the node.js code :

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

var db = admin.firestore();
exports.signUp = functions.https.onCall((data, context) => {
  const uuid = data.uuid;
  const name = data.name;
  const gender = data.gender;
  const age = data.age;

  var docRef = db.collection('users').doc(uuid).set({
    name: name,
    gender: gender,
    age: age,
    current_point: 0,
    point_charging: 0,
    init_timestamp: admin.database.ServerValue.TIMESTAMP,
    update_timestamp: admin.database.ServerValue.TIMESTAMP
  }).then(function() {
    console.log("Document successfully written!");
    return;
  }).catch(function(error) {
    console.error("Error writing document: ", error);
    return;
  });
});

The log results of firebase functions :

However, my database is still empty. I can not find my fault. Help.


回答1:


Self Answer

I tried to insert data into the firestore via firebase functions on mobile, but I modified it to insert it directly in mobile. Inserting data through functions ... I don't know if it will not work.




回答2:


I hope it still helps. I was facing the same recently, and after reading various sources i was able to find the problem.

We have to wait for the action to complete from the firestore, as it has to handle to promise.

Using async and await solves the problem.

async function getFirestore(){

var my_var = await admin.firestore();

}



来源:https://stackoverflow.com/questions/52774986/how-to-add-data-to-cloud-firestore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!