nodejs-firestore icon indicating copy to clipboard operation
nodejs-firestore copied to clipboard

I cant increment() in my firestore.d.ts file

Open momoDragon opened this issue 7 years ago • 32 comments

I am on version:

This is the firestore.d.ts file: https://pastebin.com/eEHg83kf (i cant find increment() in there)

This is what i am trying to do: admin.firestore.FieldValue.increment(1)

momoDragon avatar Apr 05 '19 05:04 momoDragon

This is the error i am getting on firebase cloud functions logs: TypeError: admin.firestore.FieldValue.increment is not a function at exports.setAvailableItemsCount.functions.firestore.document.onUpdate (/srv/index.js:203:74) at at process._tickDomainCallback (internal/process/next_tick.js:229:7)

momoDragon avatar Apr 05 '19 05:04 momoDragon

@momoDragon I just verified that it's showing up for me:

$ npm install firebase-admin
...

$ grep "increment(" node_modules/\@google-cloud/firestore/types/firestore.d.ts
    static increment(n: number): FieldValue;

If your node_modules/\@google-cloud/firestore/types/firestore.d.ts doesn't include it, can you double check the version number at the bottom of node_modules/\@google-cloud/firestore/package.json to make sure it's 1.2.0?

mikelehen avatar Apr 05 '19 15:04 mikelehen

@momoDragon that file you've provided looks like an older version of the firesore types, perhaps try the following:

npm uninstall @google-cloud/firestore rm -rf node_modules/* npm i @google-cloud/[email protected]

:point_up: let me know if this does the trick for you :+1:

bcoe avatar Apr 06 '19 00:04 bcoe

rm -rf node_modules/* didnt work for me so i just manually deleted the file. And then reinstalled functions. works like a charm! thanks a million!

momoDragon avatar Apr 07 '19 05:04 momoDragon

Souldent just by upgrading functions by npm do the job without deleting and reinstalling? Why the extra steps?

momoDragon avatar Apr 07 '19 05:04 momoDragon

@momoDragon the extra steps should not be necessary, I'm wondering if there was something odd in your package-lock.json, which uninstalling and reinstalling would help correct.

I don't expect that you should need to do this again.

bcoe avatar Apr 07 '19 21:04 bcoe

Ok my firestore.d.ts has the increment function after the reinstall of modules.

however my console log still giving me errors TypeError: admin.firestore.FieldValue.increment is not a function at exports.setAvailableItemsCount.functions.firestore.document.onUpdate (/srv/index.js:196:73) at at process._tickDomainCallback (internal/process/next_tick.js:229:7)

momoDragon avatar Apr 08 '19 03:04 momoDragon

@momoDragon could you provide some source-code, or a link to a repository that reproduces the issue? I'm curious what operation you're attempting to perform.

FieldValue I believe might be a class, so increment would be on its prototype and not exist until you new the object.

Let me know what you're trying to achieve, and I might be able to provide some sample code.

bcoe avatar Apr 08 '19 16:04 bcoe

var docRef = admin.firestore().collection('vendors').doc(user.email);

            docRef.update({
               'availableItemsCount'  :  admin.firestore.FieldValue.increment(1),
            });

momoDragon avatar Apr 08 '19 17:04 momoDragon

@momoDragon I think there's just a slight misunderstanding happening here about the increment API (I've opened an issue to document it better); here's how you'd go about implementing a field:

const {Firestore, FieldValue} = require('@google-cloud/firestore');
async function main() {
    const firestore = new Firestore();

    const document = firestore.doc('folks/bob');

    await document.set({
      name: 'Bob',
      age: 33,
    });
    console.log('Entered new data into the document');

    // Update an existing document.
    await document.update({
      age: FieldValue.increment(33)
    });
};

main().catch(console.error);

Note that I import FieldValue from the main library, and then simply call it directly: FieldValue.increment(N).

bcoe avatar Apr 08 '19 18:04 bcoe

Btw my usage is for cloud functions node.js Getting this error from your usage: ReferenceError: FieldValue is not defined at exports.setAvailableItemsCount.functions.firestore.document.onUpdate (/srv/index.js:196:46) at at process._tickDomainCallback (internal/process/next_tick.js:229:7)

Screenshot_42 Screenshot_43

momoDragon avatar Apr 09 '19 00:04 momoDragon

cc/ @hiranya911 who owns the Admin SDK and might know if there's some reason this wouldn't be working (we added FieldValue.increment() in a recent @gcloud/firestore release, but it's apparently not showing up / working via the admin SDK).

mikelehen avatar Apr 09 '19 00:04 mikelehen

Any update on the situation?

momoDragon avatar Apr 10 '19 10:04 momoDragon

@momoDragon This is working for me. I ran "npm install firebase-admin" and then put this in a .ts file and ran it:

import * as admin from 'firebase-admin';

admin.initializeApp({...});
const db = admin.firestore();
const doc = db.doc('foo/bar');
doc.update({count: admin.firestore.FieldValue.increment(1) }).then(() => console.log('success'));

And it printed "success" and the value was incremented. If you're still having issues, perhaps you can provide an MCVE?

mikelehen avatar Apr 10 '19 13:04 mikelehen

@mikelehen I think the problem is that @momoDragon is using @google-cloud/firestore; it would seem that the admin functionality works as expected in the npm package firebase-admin, but not in the npm package @google-cloud/firestore.


@momoDragon perhaps you could use the firebase-admin library for the time being, and I'll work on talking to the right people to consolidate the two libraries.

bcoe avatar Apr 11 '19 00:04 bcoe

@momoDragon what might be very helpful at this point to help us debug further also, would be if you could provide us with either a small GitHub repository that demonstrates your issues, or a gist.

@mikelehen and I are scratching our heads trying to figure out exactly how you're initializing your admin instance.

bcoe avatar Apr 11 '19 00:04 bcoe

const admin = require('firebase-admin'); this is how i initialize

momoDragon avatar Apr 11 '19 04:04 momoDragon

If you’re still stuck, can you provide your full code and the output of ‘npm ls’?

On Wed, Apr 10, 2019 at 9:04 PM momoDragon [email protected] wrote:

const admin = require('firebase-admin'); this is how i initialize

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/googleapis/nodejs-firestore/issues/597#issuecomment-481956896, or mute the thread https://github.com/notifications/unsubscribe-auth/AAMmHE2sHmvOq7bYMQkmau_5OqrRTeKyks5vfrREgaJpZM4ceKl5 .

mikelehen avatar Apr 11 '19 04:04 mikelehen

here is the code: https://pastebin.com/EQtKDQvK

error in cloud functions logs: TypeError: admin.firestore.FieldValue.increment is not a function at exports.testIncrement.functions.https.onRequest (/srv/index.js:1032:46) at cloudFunction (/srv/node_modules/firebase-functions/lib/providers/https.js:57:9) at /worker/worker.js:783:7 at /worker/worker.js:766:11 at _combinedTickCallback (internal/process/next_tick.js:132:7) at process._tickDomainCallback (internal/process/next_tick.js:219:9)

momoDragon avatar Apr 11 '19 04:04 momoDragon

i edited my comment link ^

momoDragon avatar Apr 11 '19 04:04 momoDragon