简体   繁体   English

Typescript 错误代码:2366,Function 缺少结束返回语句且返回类型不包括“未定义”

[英]Typescript Error code: 2366, Function lacks ending return statement and return type does not include 'undefined'

I am currently working on the server/database of my project.我目前正在处理我项目的服务器/数据库。 It is currently composed of Javascript, Typescript, MongoDB, Apollo-Server, and Express.目前由Javascript、Typescript、MongoDB、Apollo-Server和Express组成。 The error above keeps coming up and I am not sure how to solve it.上面的错误不断出现,我不知道如何解决它。 Here is the code I have on my index.ts file for my database folder.这是我的 index.ts 文件中用于数据库文件夹的代码。

import { MongoClient } from "mongodb";
import { Database, Listing, Booking, User } from '../lib/types';


const url = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_USER_PASSWORD}@${process.env.DB_CLUSTER}.mongodb.net`;

export const connectDatabase = async (): Promise<Database> => {
  try {
    const client = await MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true });
    const db = client.db("main");

    return {
      bookings: db.collection<Booking>("bookings"),
      listings: db.collection<Listing>("listings"),
      users: db.collection<User>("users"),
    };
  } catch (error) {
    console.log(error);
  }
};

Any help would be greatly appreciated.任何帮助将不胜感激。

You're catching the error but then you're not returning anything from the function.您发现了错误,但是您没有从 function 返回任何内容。 That is why it's complaining.这就是它抱怨的原因。 Either remove the try/catch and handle the error in the function calling this one or return something usable to the caller.删除 try/catch 并处理调用此错误的 function 中的错误,或者将可用的内容返回给调用者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM