简体   繁体   中英

What does this error mean when deploying Firebase cloud function

I am trying to deploy my first Firebase Cloud Function but when deploying I get a confusing error

I have tried updating npm but still get same error

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

exports.sendNotification = functions.firestore.document("posts/{userID}/userPosts/{postDate}").onWrite(event => {
    const userEmail = event.params.userEmail;
    const notificationId = event.params.notificationId;

    return admin.firestore().collection("notifications").doc(userEmail).collection("userNotifications").doc(notificationId).get().then(queryResult => {
        const senderUserEmail = queryResult.data().senderUserEmail;
        const notificationMessage = queryResult.data().notificationMessage;

        const fromUser = admin.firestore().collection("users").doc(senderUserEmail).get();
        const toUser = admin.firestore().collection("users").doc(userEmail).get();

        return Promise.all([fromUser, toUser]).then(result => {
            const fromUserName = result[0].data().userName;
            const toUserName = result[1].data().userName;
            const tokenId = result[1].data().tokenId;

            const notificationContent = {
                notification: {
                    title: fromUserName + " is shopping",
                    body: notificationMessage,
                    icon: "default"
                }
            };

            return admin.messaging().sendToDevice(tokenId, notificationContent).then(result => {
                console.log("Notification sent!");
                //admin.firestore().collection("notifications").doc(userEmail).collection("userNotifications").doc(notificationId).delete();
            });
        });
    });
});

I expect that the function will be posted and will return a url that I can use in my code. However I receive this error:

src/index.ts:1:1 - error TS6133: 'functions' is declared but its value is never read.

1 import * as functions from 'firebase-functions';
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 1 error.

npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ build: `tsc`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the functions@ build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/David/.npm/_logs/2019-10-02T00_37_40_011Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code2

I have looked in /Users/David/.npm/_logs/2019-10-02T00_37_40_011Z-debug.log but it was empty.

Thank you for any help.

So I figured out how to get it to run but I do not know if this is the 'right' way to solve this issue: in the index.ts file that was generated when the project was created I had to delete the line that instantiated functions. If anyone knows why this fixed the error or has any other input on this issue I would love to hear.

Thank you

I think that you didn't save the source file before deploying. On disk, the function is probably still commented out, which would cause functions to go unused.

You can check the index file in your functions it must have commented code. So for that you need to save the file so that your functions can be read and then be deployed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM