ravendb / ravendb-nodejs-client

RavenDB node.js client
MIT License
63 stars 32 forks source link

Map/Reduce TypeError: Object has no method 'flat' #336

Closed amirbrzz1 closed 1 year ago

amirbrzz1 commented 1 year ago

In the reduce we I want use some extensions like flat, flatMap, or something new in typescript, I get this error : TypeError: Object has no method 'flat'


this.reduce((result) =>
            result
                .groupBy((c) => c.mobileNumber)
                .aggregate((groupResult) => {
                    return {
                       employerOrganisationDocumentIds: groupResult.values
                            .map((x) => x.employerOrganisationDocumentIds)
                            .flat()
                            .filter((v, i, a) => a.indexOf(v) === i),

image

if I change my code to avoid using the flat then I can create my index


this.reduce((result) =>
            result
                .groupBy((c) => c.mobileNumber)
                .aggregate((groupResult) => {
                    return {
                       employerOrganisationDocumentIds: groupResult.values
                                .map((x) => x.employerOrganisationDocumentIds)
                               .reduce((acc, val, i) => acc.concat(val), []).filter((v, i, a) => a.indexOf(v) === i),

my typescript version is 4.7.4 like the ravendb

ayende commented 1 year ago

You can add a polyfil for that as additional sources, which will provide this, no?

amirbrzz1 commented 1 year ago

thanks @ayende for your quick response I can't get ur point, Do you mean I should use something like the blow code instead of using map


        this.reduce((result) =>
            result
                .groupBy((c) => c.mobileNumber)
                .aggregate((groupResult) => {
                    return {
                        ids: groupResult.values.map(c=>c.ids).reduce((acc, val, i) => acc.concat(val), []),

or there is a config that can I import my polyfill file into Ravendb and use map directly, I prefer the second one because sometimes I should write some custom extensions for typescript it would be great if I can use them directly in the reduce

as as sample :


declare global {
    interface Array<T> {
        __flat_deDupe(this : string[][]): string[];
    }
}

Array.prototype.__flat_deDupe = function(this : string[][]) : string[] {
    return Array.prototype.concat([],this).filter((v, i, a) => a.indexOf(v) === i);
}

//and use in reduce like
ids: groupResult.values.map(c=>c.ids).__flat_deDupe()

if there is a way to register custom extensions like __flat_deDupe or even naturals like map, flat, ... , please give me clue or leave a link for more clarification thanks again

amirbrzz1 commented 1 year ago

For someone who is in a hurry and careless like me ;)

thanks @ayende