Okay so I have a collection called Addresses
. I only want to allow a user to insert into this collection under two conditions:
- The
userId
field matches their userId - The geo-lookup of the given address is valid.
I am having trouble actually denying it and responding with the appropriate error. I use node-easypost
to check the geolocation.
import Promise from "bluebird";
import {check} from "meteor/check";
import {Addresses} from "/collections/addresses";
const Easypost = require('node-easypost')(process.env.EASYPOST_KEY);
export async function validateAddress(address) {
check(address, Addresses.simpleSchema());
const createAndVerifyAsync = Promise.promisify(Easypost.Address.create_and_verify);
try {
return await createAndVerifyAsync(address);
} catch (e) {
throw new Meteor.Error(e.message.code, e.message.message); // easypost has weird error responses.
}
}
// tried with both `allow` and `.before.insert`
Addresses.before.insert(async function (userId, doc) {
try {
await validateAddress(doc);
} catch (e) {
return false;
}
});
But this doesn’t seem to work. Is there a way to add async rejection to updating and inserting into a collection?