Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting error : Cannot find name 'Contact' #7

Closed
ari89-dev opened this issue Jan 29, 2020 · 3 comments
Closed

Getting error : Cannot find name 'Contact' #7

ari89-dev opened this issue Jan 29, 2020 · 3 comments

Comments

@ari89-dev
Copy link

ari89-dev commented Jan 29, 2020

I am getting the following error : -

i  deploying functions, hosting
Running command: npm --prefix ./functions/ run lint

> functions@ lint /Users/anila/Firebase/functions
> tslint --project tsconfig.json

Running command: npm --prefix ./functions/ run build

> functions@ build /Users/anila/Firebase/functions
> tsc

src/index.ts:21:24 - error TS2304: Cannot find name 'Contact'.

21         const contact: Contact = {
                          ~~~~~~~


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/anila/.npm/_logs/2020-01-29T07_50_28_164Z-debug.log

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

Am I doing anything wrong?

@dalenguyen
Copy link
Owner

Seems like the error is Contact name. Could you please post the content of the index.ts?

@ari89-dev
Copy link
Author

Following is the content of index.ts :

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as firebaseHelper from 'firebase-functions-helper/dist';
import * as express from 'express';
import * as bodyParser from "body-parser";

admin.initializeApp(functions.config().firebase);
const db = admin.firestore();

const app = express();
const main = express();

main.use(bodyParser.json());
main.use(bodyParser.urlencoded({ extended: false }));
main.use('/api/v1', app);

const contactsCollection = 'contacts';

export const webApi = functions.https.onRequest(main);

interface Contact {
    firstName: String
    lastName: String
    email: String
}

// Add new contact
app.post('/contacts', async (req, res) => {
    try {
        const contact: Contact = {
            firstName: req.body['firstName'],
            lastName: req.body['lastName'],
            email: req.body['email']
        }

        const newDoc = await firebaseHelper.firestore
            .createNewDocument(db, contactsCollection, contact);
        res.status(201).send(`Created a new contact: ${newDoc.id}`);
    } catch (error) {
        res.status(400).send(`Contact should only contains firstName, lastName and email!!!`)
    }        
})

// Update new contact
app.patch('/contacts/:contactId', async (req, res) => {
    const updatedDoc = await firebaseHelper.firestore
        .updateDocument(db, contactsCollection, req.params.contactId, req.body);
    res.status(204).send(`Update a new contact: ${updatedDoc}`);
})

// View a contact
app.get('/contacts/:contactId', (req, res) => {
    firebaseHelper.firestore
        .getDocument(db, contactsCollection, req.params.contactId)
        .then(doc => res.status(200).send(doc))
        .catch(error => res.status(400).send(`Cannot get contact: ${error}`));
})

// View all contacts
app.get('/contacts', (req, res) => {
    firebaseHelper.firestore
        .backup(db, contactsCollection)
        .then(data => res.status(200).send(data))
        .catch(error => res.status(400).send(`Cannot get contacts: ${error}`));
})

// Delete a contact 
app.delete('/contacts/:contactId', async (req, res) => {
    const deletedContact = await firebaseHelper.firestore
        .deleteDocument(db, contactsCollection, req.params.contactId);
    res.status(204).send(`Contact is deleted: ${deletedContact}`);
})

export { app };

@dalenguyen
Copy link
Owner

I'm not sure what happened yet. You can remove

interface Contact {
    firstName: String
    lastName: String
    email: String
}

And delete the others that relevant to Contact.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants