The attribute 'id' is not found in the data type 'Promise<FirmDoc>'" // "The function '() => Promise<string[]>' cannot be assigned to the function '() => string[]'

My current project involves creating a "Firm" microservice that handles firm registrations and user sign-ups within the firm. However, I'm encountering an issue while running tests on my route handlers where I get an error stating "Property 'id' does not exist on type 'Promise'". Can anyone point me in the right direction?

After marking the global.signin function as async and making changes like adding await buildFirm() and replacing return [`session=${base64}`]; with ...

  const cookie = [`session=${base64}`];
  return new Promise<string[]>((resolve, reject) => {
    resolve(cookie);
  })

The previous error regarding 'id' disappears, but now I encounter a new error related to global.signin saying "Type '() => Promise<string[]>' is not assignable to type '() => string[]'. Type 'Promise<string[]>' is missing the following properties from type 'string[]': length, pop, push, concat, and more."

setup.ts (test setup):

import { MongoMemoryServer } from "mongodb-memory-server";
import mongoose from "mongoose";
import jwt from "jsonwebtoken";
import { Firm, FirmDoc } from '../models/firm';
declare global {
  var rootsignin: () => string[];
}

declare global {
  var signin: () => string[];
}

jest.mock("../nats-wrapper");

let mongo: any;
beforeAll(async () => {
  jest.setTimeout(10000)
  process.env.JWT_KEY = "asdfasdf";
  process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

  const mongo = await MongoMemoryServer.create();
  const mongoUri = mongo.getUri();

  await mongoose.connect(mongoUri, {});
});

...
... (repeated code for brevity)

firm.ts (Firm Model):

import mongoose from 'mongoose';
import { FirmStatus } from "@mavata/common";

export { FirmStatus };

...

... (more code here)

If you have any insights or solutions to my problem, please let me. Thank you.

Answer №1

In this code snippet, you are explicitly stating that the function signin will output an array of strings.

declare global {
  var signin: () => string[];
}

As the buildFirm function operates asynchronously, it is necessary to use the await keyword before proceeding with firm.id. To properly await its completion, signin must be converted into an async function, which in turn transforms the return type into a Promise.

declare global {
   var signin: () => Promise<string[]>
}

In all likelihood, the declare global blocks may not be needed in your specific case.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to obtain a reference that can call an operator?

Is it possible to obtain a reference to an operator (like ===) in TypeScript? The reason behind this question is the following function: function dedup<T>(values: T[], equals: (a: T, b: T) => boolean): T[] { return values.reduce<T[]>((pre ...

Creating a TypeScript function that utilizes generics to automatically infer the return type

How can I create a function with a generic argument that can return any type, and have the return type inferred from its usage? I attempted the following code: type Thing<T> = <U>(value: T) => U const shouldMakeStrings: Thing<string> ...

Issues related to setting up the Ktor server with Kmongo (Kotlin Server + Mongo) configuration

Currently utilizing Ktor in conjunction with KMongo. Encountering an issue upon starting the server on localhost and initiating a simple 'get' request. The information available isn't particularly clear and comprehensive - - regarding how t ...

Is it possible to combine various SVG icons into a single component?

I am currently able to code SVGs in React-Native using typescript. This allows me to call them as individual react native components. Below is an example of my current capability: <View> <BackArrow color ="red" wid ...

A guide to efficiently passing props in a Vue.js application connected to Express with MongoDB

I am in the process of creating a simple chat application using Vue.js, Node, Express, and MongoDB. The app starts with a welcome page where users can input their names (refer to: Welcome.vue). After entering their name, users are directed to Posts.vue, pa ...

Revolutionizing the way data is updated: Angular 2 and Node JS collaborate

In my Angular 2 application, I have incorporated a dynamic navbar that displays the count of unread messages for each user. Interestingly, when a person clicks on a specific message, it is correctly marked as read in the database. However, an issue arises ...

Keeping track of important dates is ineffective using the calendar

I am facing an issue with developing a calendar that marks events on the correct dates. I am receiving the dates in the following format in PHP [ { "status": "OK", "statusCode": 200, "statusMensagem": & ...

How would you define 'Idiomatic' in the context of Idiomatic Javascript?

During his initial discussion on TypeScript, Anders repeatedly mentions the term 'idiomatic javascript'. Can you clarify the specific definition of idiomatic in this context? I've attempted to research this on Wikipedia and Stack Overflow, ...

Inspecting a union type with a TypeScript property validation

I have defined a union of two types in my code. Here are the definitions: type Generic = { subtype: undefined, user: string, text: string } type Other = { subtype:'message', text: string } type Message = Generic | Other; Within my co ...

Making changes to a single formControl in angular6 will cause the other formControl to be updated as

I have two select menus in my user interface with the same options. When I select an option in one menu, I want the other menu to display the same option, and vice versa. Currently, I am using the valueChanges method on the first formControl to update the ...

Broaden the TypeScript interface with indexed types

Imagine a straightforward indexed interface: interface FormData { [K: string]: string; } This little fella works like a charm. However, there comes a time when I want to allow a property to be an array of strings. interface AcmeFormData extends Form ...

Developing a personalized validation function using Typescript for the expressValidator class - parameter is assumed to have a type of 'any'

I'm seeking to develop a unique validation function for express-validator in typescript by extending the 'body' object. After reviewing the helpful resource page, I came across this code snippet: import { ExpressValidator } from 'expre ...

Tips for running Typescript files on a server in the background

I am currently working on a NodeJS application that consists solely of TypeScript files, without any JavaScript files. I'd like to run it on my server in the background. Any suggestions on how I can achieve this? I experimented with using the npm pa ...

The new PropTypes package is incompatible with TypeScript's React context functionality

When utilizing React.PropTypes, the code functions correctly but triggers a warning about deprecation (Accessing PropTypes via the main React package is deprecated...): import * as React from 'react'; export class BackButton extends React.Compo ...

Discover the method of obtaining a list of unique IDs within sub-array objects

Dear StackOverflow community, I am facing a similar issue to the one discussed in this post about getting only List of IDs of JSON Objects. However, my requirement is specific to performing this task with a MongoDB query due to the large amount of data I ...

Unusual class title following npm packaging

Currently, I am working on developing a Vue 3 library with TypeScript. We are using Rollup for bundling the library. Everything works as expected within the library itself. However, after packing and installing it in another application, we noticed that th ...

Contrasting characteristics of class members in JavaScript versus TypeScript

Typescript, a superset of Javascript, requires that Javascript code must function in Typescript. However, when attempting to create class members in a typescript file using the same approach as Javascript, an error is encountered. CODE :- script.ts (types ...

"Preparing the test database by cleaning it out before kicking off the integration test using Jest's Sup

In my user.test.js file, I have the following code: const request = require('supertest'); let server ; const {User} = require('./../../models/user'); describe('/users', () => { beforeEach(() => server = requi ...

Unexpected actions in the while loop within the workplace script

This code snippet I have been using to update cell A1 doesn't seem to be working: function main(workbook: ExcelScript.Workbook) { let worksheet = workbook.getWorksheet("Sheet1"); var i: number = 0; while (true) { worksheet.getR ...

The model `user` does not have a primary key attribute specified. It is required for all models to have a primary key attribute defined

I have defined a waterline model below: var Waterline = require('Waterline'); var bcrypt = require('bcrypt'); var User = Waterline.Collection.extend({ identity: 'user', datastore: 'myMongo', autoPK: false, attribut ...