how to check if a variable is Firestore Document Data type? - node.js

I am using Typescript and NodeJS for my Google Cloud Function
if I get a document from firestore like this
const result = await db.doc(`events/${eventID}`).get();
const myData = result.data();
then the type of myData is FirebaseFirestore.DocumentData like this
in other part of my code,
I need to check, if a variable has the type of FirebaseFirestore.DocumentData or not using the code below
if ( myVariable instanceof FirebaseFirestore.DocumentData ) {
// do something
}
but I have an error like this
Property 'DocumentData' does not exist on type 'typeof
FirebaseFirestore'
so how to check if my variable is a type of Firestore Document Data?
I see this answer , and it needs to import something so I can access it. how to do something like that for FirebaseFirestore.DocumentData ?

As with your other question you are mixing up Typescript types and JavaScript objects.
FirebaseFirestore.DocumentData refers to a type that is used by the TypeScript compiler as a compile time check. TypeScript compiles down to JavaScript and JavaScript doesn't know about those types during runtime. So the only way to check if the returned data is the Document Data you have to check for the existence of fields or check for undefined.

Related

How to get the type/interface of an unknown imported function in TypeScript

I've heard that it is bad practice to set an expected type of a variable to any.
However, I don't understand, how I can get a return type of an imported function, which returns an object, which is only used in the library.
As an example, if I would like to use the crypto.createCipheriv() function provided by Node.js, which returns a Cipher object as stated in the docs, I would not known how to give a variable this type.
const variable: ReturnType<crypto.createCipheriv>;

Test if a property in objectContaining is an Array or undefined

I am using Jest (TypeScript on Node.js) for unit testing and would like to check if a specific property is either an Array or undefined using expect.objectContaining(). This property may or may not exist in the object, hence why I want my test to include both cases.
My initial idea was to use
propertyInQuestion: expect.any(Array || undefined)
inside objectContaining(), but it fails because an Array is expected. When I checked the documentation, it stated that an expected object must be a subset of the received object, so it contains properties that are present in the expected object. So what happens when a property is either present and must be of an Array type or is undefined and therefore not present in the object?
At the moment, test looks something like this
expect(someObject).toEqual(
expect.objectContaining({
propertyInQuestion: expect.any(Array),
something: expect.any(Array),
})
);
and the property I'd like to check is propertyInQuestion, which can either be an Array or undefined.
Any sort of help is appreciated. Thank you!

Mongoose schema method not working after select paramter

I have a mongoose model with a method that checks a field and dynamically returns a value, and is called in an HTML template.
This gave me a problem when cleaning up routes to only select fields needed on the page.
After including the select parameter as in Model.find({}, 'select parameter', cb..., this schema method started failing, despite including in the select parameter the property this schema method checks.
Whats up with this, is there a way around it?
The schema method is defined inside thingSchema.methods: { ... ...
and looks for this.thing.length
which is included in the select parameter 'thing, otherThing, thingyThing, thingestThing'
and is called in the html template like thing.getThing(), which will throw error can't find prop length of undefined.
It turns out there is a problem with the string type for the select parameter.
Use the object type instead: {thing:1,otherThing:1,things:1}.

SuiteScript 2.0 search.Type is undefined

I am trying something like below:
require(['N/search'],
function(search)
{
var mySearch = search.create({
type : search.Type.FOLDER,
columns : ['internalid'],
filters : [ 'internalid', 'anyof', ID]
});
mySearch.run();
});
I get an error for search.Type.FOLDER that search.Type is undefined and so cannot find FOLDER of undefined
I was able to do a workaround by writing a type as 'folder' that worked, but, why is this enum not defined if it is documented in NetSuite's help.
I tried even logging all keys using Object.keys and the returned array does not contains Type key.
Has anyone tried this or if anyone can point out if something is wrong with my code?
I don't see anything wrong with your code, and I confirmed in my own instance that the module brought in by N/search does not include a Type enum. Including the N/record module does have correctly have the Type enum, so if you want to avoid the magic string 'folder', you could import N/record and use record.Type.FOLDER instead.
It's not ideal, as what you are doing should work, but it seems there must be a bug in the search module where they're not properly returning the Type enum.

Returning a module in RequireJS

I'm refactoring a large javascript codebase to use RequireJS. Unfortunately, many of the files I'm dealing with are not object-oriented, and cannot return an object without significant modification. Is there a more efficient way to give 'dependent' modules access to the functions and variables contained in a module (without returning an object) ?
I have read about using the exports syntax for defining modules, but it is very unclear whether that would be a valid solution for this situation.
In a defined module, the exports object is what gets exported from the module and passed to whatever module requires it.
Consider this:
define(["exports"], function(exports){
exports.myCustomFunction = function(){};
exports.myCustomObject = {};
exports.myCustomVariable = true;
})
This module will place all the disparate functions and/or objects that you want made public onto the exports object.
At this point RequireJS will use that exports object to pass to a module that requires it:
require(["nameOfCustomModule|filename"], function(myCustomModule){
//evaluates to true
console.log(myCustomModule.myCustomVariable);
})
Here's a simple fiddle. Just bring up your console and you will see the value logged there. http://jsfiddle.net/xeucv/
Hope this clears it up a bit!

Resources