本文介绍了使用函数和 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()
});

更新

因此,使用 Web 等效项似乎可行: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.字段值;

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 06:58