使用函数和Firestore时未定义FieldValue [英] FieldValue undefined when using functions and Firestore

查看:58
本文介绍了使用函数和Firestore时未定义FieldValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下功能:

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

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

Every time i run it I get the following error:

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

文档我真的看不到我在做什么错了?

Looking at the docs I cant really see what I am doing wrong?

// 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().但这似乎是一个错误,因为我处于nodejs环境中? Firebaser是否可以确认或拒绝这种情况?我很乐意提交错误报告.

So, using the web equivalent seems to work: admin.firestore.FieldValue.delete(). But that seems like a bug since i'm in a nodejs environment? Can any Firebaser confirm or deny wether that is the case or not? I'd gladly file a bug report.

推荐答案

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

Turns out it was a mistake in the documentation, the correct import should have been const FieldValue = require('firebase-admin').firestore.FieldValue;

更新

应该说Firebase在几个小时内做出了回应,并正在尽快更正文档.

It should be said that Firebase responded within hours and are correcting the docs asap.

这篇关于使用函数和Firestore时未定义FieldValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆