Post()

post({ ...params })

The post method allows you to create a new document within the specified collection. The request body should contain the data for the new document, adhering to the schema defined in the config.json file. If pick or omit rules are defined, only the specified fields will be included or excluded in the request.

You can customize the result fields using the project property in config.json.

const todo = await client.collection("todos").post({
    "title": "New Task",
    "priority": "high"
});
console.log(todo);

// result:
// {
//   "_id": "60d3b41d3d8b6e5b1f0f3b8c",
//   "title": "New Task",
//   "priority": "high"
//   "__v": 0
// }

Add Current user to new Document

To include the current user as a user property in a new document, define the user property in the config.json schema as follows:

"posts": {
    "schema": {
        "title": {
            "type": "string"
        },
        "user": {
            "type": "string",
            "default": "$.auth._id"
        }
    },
    "permissions": {
        "user": {
            "post": {
                "pick": [
                    "title",
                    "priority"
                ]
            }
        }
    }
}

In this example, only authenticated users can create a todo, and the user field is automatically set to the authenticated user's _id from the JWT.

import Client from "noonjs-client";
const token = { access: "eyJ0eXBiOiJ3V1QiaCJhbGciOiJIUzI1..." }; // JWT token
const client = new Client("http://localhost:3000", { token });
const todo = await client.collection("todos").post({
    "title": "New Task",
    "priority": "high"
});
console.log(todo);

// result:
// {
//   "_id": "60d3b41d3d8b6e5b1f0f3b8c",
//   "user": "640b8e8e4a9e5f56c678b8d9",
//   "title": "New Task",
//   "priority": "high"
//   "__v": 0
// }
Edit this page on Github
© 2025 kav3.com. Crafted with and dedication.