函数返回未定义,预期Promise或值Firebase日志错误

时间:2018-04-17 12:01:14

标签: node.js firebase google-cloud-functions

我尝试使用firebase-function和node.js在我的应用上添加推送通知,并且所有工作正常,就像我收到发件人的通知一样。但我唯一担心的是日志给了我这个错误

Function returned undefined, expected Promise or value 

这是我的代码:

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

exports.sendNotification = functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite((change, context) => {

    const user_id = context.params.user_id;
    const notification_id = context.params.notification_id;

    console.log('We have a notification : ', user_id);

    const afterData = change.after.val();

    if (!afterData){
        return console.log('A notification has been deleted from the database', notification_id);
    }

    const fromUser = admin.database().ref(`/notifications/${user_id}/${notification_id}`).once('value');
    return fromUser.then(fromUserResult => {

        const from_user_id = fromUserResult.val().from;


        console.log('You have a new notification from: ', from_user_id);

        const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');

        return deviceToken.then(result => {

            const token_id = result.val();

            const payload = {
                notification: {
                    title: "New Friend Request",
                    body: "You've received a new Friend Request",
                    icon: "default"
                }
            };

            return admin.messaging().sendToDevice(token_id, payload).then(response => {
                console.log('This was the notification feature');
            });

        });

    });

});

我应该在这里返回什么以及它将放在哪里?我目前正在使用最新的firebase CFM 1.0版

1 个答案:

答案 0 :(得分:2)

此行可能导致错误消息:

return console.log('A notification has been deleted from the database', notification_id);

点击此行时,您有效地从该函数返回undefined,因为console.log()返回的是return null。相反,您应该在日志之后 <tr> <th style="width: 30$">ID</th> <th style="width: 30$">Created By</th> <th style="width: 30$">Estimate time</th> <th style="width: 30$">Timesheet time</th> <th style="width: 30%">Updated By</th> </tr> <% try { Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/testraildump","roo t","root"); String Query="SELECT id, createdby, estimatetime, executionhours, updatedby from testraildumptable inner join timesheet on testraildumptable.updatedby = timesheet.updatedby where updatedby='"+request.getParameter("search")+"'"; Statement stm=conn.createStatement(); ResultSet rs=stm.executeQuery(Query); while(rs.next()) { %> <tr> <td><%=rs.getString("id")%></td> <td><%=rs.getString("createdby")%></td> <td><%=rs.getString("estimatetime")%></td> <td><%=rs.getString("executionhours")%></td> <td><%=rs.getString("updatedby")%></td> </tr> <% } }