Node.js ts(2355)在尝试返回文档数组NodeJS、Mongoose、Typescript时
我正在使用基于餐馆的MongoDB样本数据处理Typescript、Mongoose、NodeJS和Express 我试图创建简单的CRUD操作,目的是返回所有餐厅,然后返回特定的餐厅,最终过滤/排序等 请参见下面的功能:Node.js ts(2355)在尝试返回文档数组NodeJS、Mongoose、Typescript时,node.js,typescript,mongoose,Node.js,Typescript,Mongoose,我正在使用基于餐馆的MongoDB样本数据处理Typescript、Mongoose、NodeJS和Express 我试图创建简单的CRUD操作,目的是返回所有餐厅,然后返回特定的餐厅,最终过滤/排序等 请参见下面的功能: const fetchRestaurants = async (request: Request, response: Response): Promise<RestaurantInterface[]> => { try { const rest
const fetchRestaurants = async (request: Request, response: Response): Promise<RestaurantInterface[]> => {
try {
const restaurants: RestaurantInterface[] = await RestaurantModel.find();
response.status(200).json({
restaurants
});
} catch (error) {
throw error;
}
}
这是意料之中的,因为函数实际上返回void。当您编写
函数名():Promise
时,它意味着您应该返回一些东西。而您只是调用Express的json
方法。这是意料之中的事
// in another file
export interface RestaurantInterface {
name: string,
borough: string,
cuisine: string,
restaurant_id: string
address: Address,
grades: Grades[],
};