使用函数和Firestore时,FieldValue未定义

时间:2017-11-03 09:08:07

标签: javascript firebase google-cloud-functions google-cloud-firestore

我有以下功能:

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

module.exports = functions.firestore
  .document('students/{studentId}')
  .onDelete(event => {
    const student = event.data.previous.data();
    const { id, semester } = student;
    const classId = student.class;
    const deleteObj = {};
    deleteObj[id] = FieldValue.delete(); //Line 12, this is where the error orccurs
    return admin
      .firestore()
      .collection('semesters')
      .doc(semester)
      .collection('students')
      .doc(classId)
      .update(deleteObj);
  });

每次运行时都会出现以下错误:

TypeError: Cannot read property 'delete' of undefined
    at module.exports.functions.firestore.document.onDelete.event (/user_code/deleteStudent.js:12:37)

看着docs我真的看不出我做错了什么?

// Get the `FieldValue` object
var FieldValue = require("firebase-admin").FieldValue;

// Create a document reference
var cityRef = db.collection('cities').doc('BJ');

// Remove the 'capital' field from the document
var removeCapital = cityRef.update({
    capital: FieldValue.delete()
});

更新

因此,使用等效的网络似乎有效:admin.firestore.FieldValue.delete()。但这似乎是一个bug,因为我在nodejs环境中?任何Firebaser都可以确认或拒绝这种情况吗?我很乐意提交错误报告。

2 个答案:

答案 0 :(得分:24)

原来这是文档中的错误,正确的导入应该是const FieldValue = require('firebase-admin').firestore.FieldValue;

<强>更新

应该说Firebase会在几小时内回复,并且正在尽快纠正文档。

答案 1 :(得分:3)

成功了!

需要firebase-admin

const admin = require('firebase-admin')

添加此常量

const fieldValue = admin.firestore.FieldValue; 

现在使用

fieldValue.delete()

更多参考:otcollect.com/post