这是我第一次使用Oak和Deno。我可以从https://www.robinwieruch.de/deno-oak-rest-api和文档中学习。我想通过此脚本(server.ts)使用REST-API deno。
import { v4 } from 'https://deno.land/std/uuid/mod.ts';
import {
Application,
Router,
helpers,
} from 'https://deno.land/x/oak/mod.ts';
interface User {
id: string;
username: string;
}
const users = new Map<string, User>();
users.set('1', {
id: '1',
username: 'Robin Wieruch',
});
users.set('2', {
id: '2',
username: 'Dave Davids',
});
interface Message {
id: string;
text: string;
userId: string;
}
const messages = new Map<string, Message>();
messages.set('1', {
id: '1',
text: 'Hello World',
userId: '1',
});
messages.set('2', {
id: '2',
text: 'By World',
userId: '2',
});
const port = 8000;
const app = new Application();
const router = new Router();
router.get('/session', (ctx) => {
ctx.response.body = users.get(ctx.state.me.id);
});
router.get('/users', (ctx) => {
ctx.response.body = Array.from(users.values());
});
router.get('/users/:userId', (ctx) => {
const { userId } = helpers.getQuery(ctx, { mergeParams: true });
ctx.response.body = users.get(userId);
});
router.get('/messages', (ctx) => {
ctx.response.body = Array.from(messages.values());
});
router.get('/messages/:messageId', (ctx) => {
const { messageId } = helpers.getQuery(ctx, { mergeParams: true });
ctx.response.body = messages.get(messageId);
});
router.post('/messages', async (ctx) => {
const id = v4.generate();
const {
value: { text },
} = await ctx.request.body();
messages.set(id, {
id,
text,
userId: ctx.state.me.id,
});
ctx.response.body = messages.get(id);
});
router.delete('/messages/:messageId', async (ctx) => {
const { messageId } = helpers.getQuery(ctx, { mergeParams: true });
const isDeleted = messages.delete(messageId);
ctx.response.body = isDeleted;
});
app.use(async (ctx, next) => {
ctx.state = { me: users.get('1') };
await next();
});
app.use(router.allowedMethods());
app.use(router.routes());
app.addEventListener('listen', () => {
console.log(`Listening on: localhost:${port}`);
});
await app.listen({ port });
使用deno run --allow-net --allow-read server.ts
运行它之后,出现以下错误:$ deno run --allow-net --allow-read server.ts
Check file:///home/astrid/git/javascript/deno/deno-oak-rest-api/server.ts
error: TS2339 [ERROR]: Property 'text' does not exist on type 'Promise<any> | Promise<URLSearchParams> | FormDataReader | Promise<string> | Promise<Uint8Array> | undefined'.
value: { text },
~~~~
at file:///home/astrid/git/javascript/deno/deno-oak-rest-api/server.ts:77:14
这是我的版本$ deno --version
deno 1.3.3
v8 8.6.334
typescript 4.0.2
如何解决这个错误? 最佳答案
参见https://github.com/oakserver/oak/issues/239
关于typescript - 错误: TS2339 [ERROR]: Property x does not exist … on Deno compile,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63818894/