Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
46
rated 0 times [  49] [ 3]  / answers: 1 / hits: 28979  / 7 Years ago, mon, november 6, 2017, 12:00:00

I'm developing Server with Firebase.



I had copied Google Developer's Video on Youtube.



It works well, but on log there is an error:




Function returned undefined, expected Promise or value




It says function returned undefined, but I make function return a promise `set``



How can I solve this?



function sanitize(s) {
var sanitizedText = s;
console.log('sanitize params: ', sanitizedText);
sanitizedText = sanitizedText.replace(/bstupidb/ig, wonderful);
return sanitizedText;
}
exports.sanitizePost = functions.database
.ref('/posts/{pushId}')
.onWrite(event => {
const post = event.data.val();
if (post.sanitized) return;

console.log('Sanitizing new post', event.params.pushId);
console.log(post);
post.sanitized = true;
post.title = sanitize(post.title);
post.body = sanitize(post.body);
return event.data.ref.set(post);
})


I'm beginner of Firebase, Nodejs.


More From » node.js

 Answers
29

As Frank indicates in his comment on your post, the return statement that is producing the warning is this one:



if (post.sanitized) return;


The warning can be silenced by returning a dummy value (e.g. null, false, 0). The value is not used.



Earlier versions of Cloud Functions did not complain when a function exited using a return statement with no value. That explains why you see return; in the video you linked and in the documentation. The comment on the question by Firebaser Frank van Pufeelen, explains why the change was made.



The simplest way to eliminate the warning is to add a return value, as Frank suggested:



if (post.sanitized) return 0;


Another option is to change the trigger from onWrite() to onCreate(). Then the function will not be invoked when the post is sanitized and the check that produces the warning is not needed:



exports.sanitizePost = functions.database
.ref('/test/{pushId}')
.onCreate(event => { // <= changed from onWrite()
const post = event.data.val();
//if (post.sanitized) return; // <= no longer needed

console.log('Sanitizing new post', event.params.pushId);
console.log(post);
//post.sanitized = true; // <= not needed when trigger is onCreate()
post.title = sanitize(post.title);
post.body = sanitize(post.body);
return event.data.ref.set(post);
});

[#56005] Thursday, November 2, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ramiro

Total Points: 431
Total Questions: 96
Total Answers: 105

Location: Northern Ireland
Member since Mon, Nov 14, 2022
2 Years ago
ramiro questions
Thu, May 7, 20, 00:00, 4 Years ago
Tue, Apr 28, 20, 00:00, 4 Years ago
Sun, Feb 16, 20, 00:00, 4 Years ago
;