How to use HTTP REQUEST PUT with odata.PUT to update database entries?

Viewed 71

I need a little bit of guidance trying to use odata.PUT to update entries in a DB (mongoDB). I'm having difficulties properly calling the @odata.PUT to be able to update previously created with @odata.POST.

I've tried accessing the query parameters from HTTP Request with @odata.query but to no avail.

import { ODataController, Edm, odata, getQueryParameter} from "odata-v4-server";
import { User } from "../model/User";
import "reflect-metadata";
import { ObjectID } from "mongodb";
import connection from "../connection";

@odata.type(User)
@Edm.EntitySet("User")
export class UsersController extends ODataController {
@odata.GET
    async findOne(@odata.key key: string): Promise<User> {
        const connect = await connection();
        return connect.getRepository(User).findOne({_id:key});
    }
    @odata.POST
    async create(){
        const connect = await connection();
        var key = new ObjectID();
        connect.getRepository(User).save({_id: key.toString()});
    }
    @odata.PUT
    async insert(@odata.query params){
        console.log(params);

    }
    @odata.DELETE
    async remove(@odata.key key:string) {
        const connect = await connection();
        await connect.getRepository(User).delete({username:key})
    }


}
1 Answers

Please refer this link. In your code, for update, you have mentioned @odata.PUT, instead of that use @odata.PATCH and try.

Your code should be

@odata.PATCH
    async update(@odata.query params:ODataQuery){
        console.log(params);

    }
Related