Question: I want to create a stored procedure or database function to delete some entries and return their IDs to the client. I started off with this `FUNCTION`:

```
CREATE FUNCTION dbo.FixHangingCarriers ()
RETURNS @returntable TABLE
(
    ID_Plant INT NOT NULL,
    ID_ChargeCarrier INT NOT NULL
)
AS
BEGIN;
    -- Declare a temporary table
    DECLARE @EntriesToDelete TABLE (
        ID_ChargeCarrier INT NOT NULL,
        ID_Storage INT NOT NULL,
        StoredOn DATETIME2(2) NOT NULL
    );

    -- Select all the entries that should be deleted
    INSERT INTO @EntriesToDelete
    SELECT
        ID_ChargeCarrier,
        ID_Storage,
        StoredOn
    FROM dbo.CurrentStorage
    WHERE ID_StorageType = 4
    AND StoredOn < DATEADD(MINUTE, -30, GetDate());

    -- Return immediately, if there is currently nothing to delete
    IF (SELECT 1 FROM @EntriesToDelete) = 1
        RETURN;

    -- Delete hanging entries
    DELETE ccs
    FROM dbo.ChargeCarrier_Storage ccs
    INNER JOIN @EntriesToDelete d ON
        d.ID_ChargeCarrier = ccs.ID_ChargeCarrier AND
        d.ID_Storage = ccs.ID_Storage AND
        d.StoredOn = ccs.StoredOn;

    -- Prepare return table
    INSERT INTO @returntable
    SELECT cs.ID
    FROM @EntriesToDelete d
    INNER JOIN dbo.ChargeCarriersCurrentlyInPlant cp ON
        cp.ID_ChargeCarrier = d.ID_ChargeCarrier;

    -- Return deleted entries to caller
    RETURN;
END;

```

Obviously this doesn't work, because SQL functions cannot delete anything. I wanted to change it into a `PROCEDURE`, but then I saw, that procedures naturally seem not to be able to return a table. How can I solve this issue?

Answer: 
After you create the `schema` you need to create a Model **FROM** that schema. 

Example from [MDN](https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/mongoose):

```
// Define schema
var Schema = mongoose.Schema;

var SomeModelSchema = new Schema({
  a_string: String,
  a_date: Date
});

// Compile model from schema
var SomeModel = mongoose.model('SomeModel', SomeModelSchema );

```

Now after you create the model you can use `SomeModel.create`

**EDIT**:
`line[1]` will always return undefined because you are using `callbacks` and only way to get value out of callback is either push another callback(I would really discourage that). But best way is to use `Promises` now mongoose by default supports `Promises. So, basically for promises it will be,

```
// user functions written

async function createUser(name, email, password){
  try {
    return await User.create({ name: name, email: email, password: password });
  } catch (err) {
    throw err.message;
  }
}

```

In the router adda `await`:

```
const user = await userManager.createUser(name, email, password);

```
Something I ran into was you need to pass in an empty object if your not setting any fields - i.e.

Good: Model.create({})

Bad: Model.create()