【问题标题】:Error occurred while parsing your function triggers解析函数触发器时出错
【发布时间】:2018-07-20 01:49:08
【问题描述】:

The following error is shown while deploying firebase function. 我尝试初始化 firebase 功能。 我还仔细检查了 index.js 文件。 我是部署 firebase 功能的新手,所以请帮助我。

index.js 如下:

const functions = require('firebase-functions');

                                                                 // replaces keywords with emoji in the "text" key of messages
                                                                 // pushed to /messages
exports.emojify =
    functions.database.ref('/messages/{pushId}/text')
    .onWrite(event => {
                                                                 // Database write events include new, modified, or deleted
                                                                 // database nodes. All three types of events at the specific
                                                                 // database path trigger this cloud function.
                                                                 // For this function we only want to emojify new database nodes,
                                                                 // so we'll first check to exit out of the function early if
                                                                 // this isn't a new message.

                                                                 // !event.data.val() is a deleted event
                                                                 // event.data.previous.val() is a modified event
        if (!event.data.val() || event.data.previous.val()) {
            console.log("not a new write event");
            return;
        }

                                                                 // Now we begin the emoji transformation
        console.log("emojifying!");

                                                                 // Get the value from the 'text' key of the message
        const originalText = event.data.val();
        const emojifiedText = emojifyText(originalText);

                                                                 // Return a JavaScript Promise to update the database node
        return event.data.ref.set(emojifiedText);
    });

                                                                 // Returns text with keywords replaced by emoji
                                                                 // Replacing with the regular expression /.../ig does a case-insensitive
                                                                 // search (i flag) for all occurrences (g flag) in the string
function emojifyText(text) {
    var emojifiedText = text;
    emojifiedText = emojifiedText.replace(/\blol\b/ig, "????");
    emojifiedText = emojifiedText.replace(/\bcat\b/ig, "????");
    return emojifiedText;
}

【问题讨论】:

  • 请不要在您的问题中放置代码和错误的图像。相反,请复制文本并将其作为代码块添加到问题中,以便于阅读和搜索。

标签: firebase google-cloud-functions


【解决方案1】:

请检查当前的 documentation 触发器,特别是 migration 从 Beta 版到 1.0 版。

event.data.previous.val() 已更改为 change.before.val()

event.data.val() 已更改为 change.after.val()

此外,Promise 语句更改为: return change.after.ref.parent.child('text').set(emojifiedText);

完整的 index.js 如下所示:

const functions = require('firebase-functions');

// replaces keywords with emoji in the "text" key of messages
// pushed to /messages

exports.emojify=
	functions.database.ref('/messages/{pushId}/text')
	.onWrite((change,context)=>{
		
		// Database write events include new, modified, or deleted
        // database nodes. All three types of events at the specific
        // database path trigger this cloud function.
        // For this function we only want to emojify new database nodes,
        // so we'll first check to exit out of the function early if
        // this isn't a new message.
		
		// Only edit data when it is first created.		
		if (change.before.exists()){
			return null;
		}
		
		// Exit when the data is deleted.
		if (!change.after.exists()){
			return null;
		}
		
		// Now we begin the emoji transformation
		console.log("emojifying!");
		
		//Get the value from the 'text' key of the message
		const originalText = change.after.val();
		const emojifiedText = emojifyText(originalText);

		//Return a JavaScript Promise to update the database nodeName
		return change.after.ref.parent.child('text').set(emojifiedText);
	});
	
// Returns text with keywords replaced by emoji
// Replacing with the regular expression /.../ig does a case-insensitive
// search (i flag) for all occurrences (g flag) in the string

function emojifyText(text){
	var emojifiedText=text;
	emojifiedText=emojifiedText.replace(/\blol\b/ig,"?");
	emojifiedText=emojifiedText.replace(/\bcat\b/ig,"?");
	return emojifiedText;
}

【讨论】:

    猜你喜欢
    • 2020-03-19
    • 1970-01-01
    • 2018-10-20
    • 1970-01-01
    • 2017-08-19
    • 2018-08-03
    • 2019-06-12
    • 1970-01-01
    相关资源
    最近更新 更多