首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何解决此Firebase云函数错误:无法读取属性

如何解决此Firebase云函数错误:无法读取属性
EN

Stack Overflow用户
提问于 2018-04-25 08:54:05
回答 2查看 738关注 0票数 0

我遵循一个教程,在这里我要向我的项目中添加一些Firebase函数(步骤5)。我已经成功地将我的云功能部署到了firebase,但是当我在Firebase数据库控制台中手动添加一个新产品时,什么都不会发生。我发现触发了Firebase云函数,但它得到了一个错误:"TypeError:无法读取未定义的属性'productId‘“

我做错了什么?

代码语言:javascript
运行
AI代码解释
复制
const functions = require('firebase-functions');

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


    exports.sendMessage = functions.firestore
        .document('products/{productId}')
        .onCreate(event => {

        const docId = event.params.productId; // <-- error here

        const name = event.data.data().name;
        const productRef = admin.firestore().collection('products').doc(docId)
        return productRef.update({ message: `Nice ${name}! - Love Cloud Functions`})

    });
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-25 10:52:53

好的。因此,由于Stevensson的回答告诉我语法已经过时,我现在有了一个解决方案:

代码语言:javascript
运行
AI代码解释
复制
const admin = require('firebase-admin');
const functions = require('firebase-functions');
admin.initializeApp(functions.config().firebase);

var db = admin.firestore();

exports.sendMessage = functions.firestore
    .document('products/{productId}')
    .onCreate((snapshot, context) => {

       const docId = context.params.productId;

       const productRef = db.collection('products').doc(docId)
       return productRef.update({ message: `Nice ${name}!`})
    });
票数 1
EN

Stack Overflow用户

发布于 2018-04-25 08:59:08

那个教程一定过时了。在Functions发布1.0版时,有些地方发生了变化。您可以阅读有关这些更改的这里

数据库触发器现在传递两个参数,而不是一个参数。新的上下文参数包含引用路径中通配符的值:

代码语言:javascript
运行
AI代码解释
复制
exports.sendMessage = functions.firestore
    .document('products/{productId}')
    .onCreate((snapshot, context) => {

    const docId = context.params.productId;

如果您想继续该教程,则必须手动将其所有旧内容转换为新内容。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50027565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档