解析函数触发器时出错

时间:2018-02-28 07:00:51

标签: firebase google-cloud-functions

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

1 个答案:

答案 0 :(得分:1)

请检查触发器上当前的documentation,尤其是从Beta到版本1.0的migration

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

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

此外,Promise语句更改为: 返回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;
}