WebStorm 11 unrecognized MongoDb (with mongoose) functions - mongodb

WebStorm gives me a warning of "Unrecognized function or method" on functions like:
Schema.find() [find() not recognized]
Schema.aggregate() [aggregate() not recognized]
Schema.findOneAndUpdate() [findOneAndUpdate() not recognized]
I've tried to Enabling the NodeJs core libraries and to install
mongodb-DefinitelyType
mongoose-DefinitelyType
mongoose-auto-increment-DefinitelyType
mongoose-deep-populate-DefinitelyType
mongoose-DefinitelyType
mongoose-mock-DefinitelyType
under Preferences > JavaScript > Libraries
But this has not solved my problem. Does someone knows a solution?

This issue is tracked as https://youtrack.jetbrains.com/issue/WEB-17099; please see https://youtrack.jetbrains.com/issue/WEB-17099#comment=27-1441265 for possible workaround

This is possible solution working for me without any issues.
Moving the relative path out of the require() statement as follows.
const PATH = '../models/';
const User = require(PATH + 'user');
Alternatively
Do not import Schema separately.
Just import mongoose like this
const mongoose = require('mongoose');
and use mongoose.Schema to access Schema

I don't know the reason but somehow this works:
export
module.exports.User = User; // your model
import
const User = require("../dbSchema/user.js").User;

Note that the schema is inserted correctly. Don't forget to check your Schema.
Sample
module.exports = mongoose.model('SampleCollection', SampleSchema);

Related

Module '#google-cloud/firestore' is not listed as dependency in package.json

I am using TypeScript for writing my Firebase Cloud Functions. In my code I need to refer firestore data types like CollectionReference, QuerySnapshot etc. For example:
export const Employee: CollectionReference = admin.firestore().collection('Master/Admin/Employees');
or
async function convertToCsv(snapshot: QueryDocumentSnapshot[])
To use these types in my code, I am using the following lines on the top of the file (.ts):
import { QueryDocumentSnapshot, Timestamp, Query, CollectionReference } from '#google-cloud/firestore';
This code works fine when I run firebase function locally on my PC using:
firebase serve --only functions
But when I try to deploy the same code on my firebase server, I get the following tslint error and code deployment fails.
Module '#google-cloud/firestore' is not listed as dependency in package.json
if I add the dependency in package.json
"dependencies": {
"body-parser": "^1.18.3",
...
"#google-cloud/firestore": "~1.0.1"
},
My functions get deployed, but on execution, I get the following error:
Error: Argument "value" is not a valid QueryValue. Detected an object of type "Timestamp" that doesn't match the expected instance. Please ensure that the Firestore types you are using are from the same NPM package.
at Validator.(anonymous function).err [as isQueryValue] (/user_code/node_modules/firebase-admin/node_modules/#google-cloud/firestore/build/src/validate.js:93:27)
at Query.where (/user_code/node_modules/firebase-admin/node_modules/#google-cloud/firestore/build/src/reference.js:916:25)
at /user_code/lib/services/reports/index.js:53:22
The compiled java script line where it fails is (see **):
const firestore_1 = require("#google-cloud/firestore");
let query = collection
.where('divisionCode', '==', divisionCode)
**.where('zzLastUpdateOn', '>=', firestore_1.Timestamp.fromDate(startDate))**
.where('zzLastUpdateOn', '<=', firestore_1.Timestamp.fromDate(endDate))
.where('zzIsDeleted', '==', false);
I really have no clue what to do here, please help. Please know I am very new to TypeScript and JavaScript world.
Please know that other functions in a same project, where i am adding value into Timestamp field is working fine. And this function also works fine in Cloud Function simulation mode on desktop (local host).
Your help will be appreciated!!!
The following change in my code has helped to clear the problem.
Instead of using the "Timestamp", I am now using "firestore.Timestamp". This change has helped me move further in my development.
Now my code looks like this:
TypeScript Code:
let query: Query = collection
.where(...)
.where('zzLastUpdateOn', '>=', firestore.Timestamp.fromDate(startDate))
.where('zzLastUpdateOn', '<=', firestore.Timestamp.fromDate(endDate))
.where(...);
And Generated Java Script Code:
let query = collection
.where(...)
.where('zzLastUpdateOn', '>=', firebase_admin_1.firestore.Timestamp.fromDate(startDate))
.where('zzLastUpdateOn', '<=', firebase_admin_1.firestore.Timestamp.fromDate(endDate))
.where(...);
My sugestion is based on https://stackoverflow.com/a/52609487/6943737 query.

How to get access to Developer: Inspect TM Scopes data? [duplicate]

I have used the tokenizer in monaco but I do not see that it is accessible in vscode. This would be helpful for completion/signature help providers, how can I tokenize a grammar?
It doesn't seem like there's an official way of doing this right now. There is an open feature request for adding the ability to retrieve tmLanguage scopes at a position here: #580
There is one potential workaround, which requires adding a dependency to the scope-info extension. This extension exposes an API of its own that other extension can use. Here's a code example posted by the author in the linked issue:
import * as api from 'scope-info'
async function example(doc : vscode.TextDocument, pos: vscode.Position) {
const siExt = vscode.extensions.getExtension<api.ScopeInfoAPI>('siegebell.scope-info');
const si = await siExt.activate();
const t1 : api.Token = si.getScopeAt(doc, pos);
}
Update: unfortunately, it looks like scope-info is no longer compatible with current VSCode versions.

why using var {google} instead of var google in google.auth.OAuth

This code is from oauth nodesjs.
I want to ask why we are using '{}' around the var google? I also tried using it without '{}' and got error OAuth2 is undefined. i can't understand what is happening here.
var {google} = require('googleapis');
var OAuth2 = google.auth.OAuth2;
To add a little to this answer - this is what's called a destructuring assignment. You can read about them here:
http://2ality.com/2015/01/es6-destructuring.html
The code you're looking at here:
const {google} = require('googleapis');
Is the same as code that looks like this:
const google = require('googleapis').google;
This is just a convenient shorthand that was added in es6. We made the change in the googleapis package when we moved towards ES modules, which don't play nicely with the export=foo style syntax. Hope this helps!
According to the Changelog from google-api-nodejs-client, there are some changes from V26.0.0 onwards that you have to implement in your code, precisely the issue you are experiencing is mentioned. I also took a while to figure this one out...
BREAKING CHANGE: This library is now optimized for es6 modules. In previous versions you would import the library like this:
const google = require('googleapis');
In this and future versions, you must use a named import:
const {google} = require('googleapis');
You may also reference the type to instantiate a new instance:
const {GoogleApis} = require('googleapis');
const google = new GoogleApis();

ReactJS / MongoDB: export const Database (how to insert.before)?

I am using MeteorJS as my framework.
In my imports/api/collections/categories.js file, I have:
export const Photos = new Mongo.Collection('photos');
On the server side, I have:
Photos = new Mongo.Collection('photos');
How can I insert.before so all my documents have an extra field when they get added to the database?
Before I was exporting for React, I had:
Photos = new Mongo.Collection('photos');
Photos.before.insert(function(userId,doc){
doc.createdAt=Date.now();doc.status=0;
});
But that doesn't work with:
export const Photos...
Ideas how I achieve the insert before?
Okay, as it turns out, I can do what I did before:
Photos.before.insert(function(userId,doc){doc.status=0;});
I just had to bring in this package into Meteor:
matb33:collection-hooks

Connection to MongoDB from MATLAB

I want to create a connection to my database in MongoDB from Matlab R2015a. I have tried with both the drivers for C# and Java but none of them seem to work and I don't know what the problem is.
For Java:
Code:
javaaddpath('/%path%/mongodb-driver-3.0.0.jar')
import com.mongodb.*;
mongoClient = MongoClient();
db = mongoClient.getDB('myDB');
colls = db.getCollectionNames();
coll = db.getCollection('myCollection');
Error:
No appropriate method, property, or field 'getDB' for class 'MongoDB.Driver.MongoClient'.
For C#:
Code:
NET.addAssembly('%path%\CSharpDriver-2.0.0\MongoDB.Driver.dll');
import MongoDB.Driver.*;
mongoClient = MongoDB.Driver.MongoClient();
mongoServer = mongoClient.GetServer();
db = mongoClient.GetDatabase('myDB');
collection = db.GetCollection('myCollection');
Errors:
1. No appropriate method, property, or field 'GetServer' for class 'MongoDB.Driver.MongoClient'.
2. If I comment the GetServer line I get: No appropriate method, property, or field 'GetCollection' for class 'MongoDB.Driver.MongoDatabaseImpl'.
I don't know if I am missing something and it would be really helpful if I could make it work.
I have also tried with the driver for Matlab but I couldn't make it create the .dll.
Thanks.
You have to open the client with:
import com.mongodb.*;
mongoClient = MongoClient('myIP', 'myPort');
I'm using the java version with Matlab 2015b. I assume you have done the import correct. Otherwise, the MongoClient class would not be found.