如何正确使用猫鼬的预保存钩子?

时间:2019-10-09 09:38:36

标签: node.js mongodb mongoose mongoose-schema

我正在尝试使用猫鼬的预保存钩子生成一个简短的URL。

我希望在调用创建方法(仅提供完整网址)时,它会生成自己的短网址并返回一个同时包含完整网址和短网址的对象。

但是结果是一个仅包含完整URL的对象。当我登录预保存的钩子时,我看到该对象仅包含短网址。 因此它确实调用了pre-save钩子,但是不知道是在同一个对象上吗?

我该怎么做才能将短网址添加到要存储并返回的对象中?

创作:

def lambda_handler(event, context):
    # Get the object from the event and show its content type
    bucketName = event['Records'][0]['s3']['bucket']['name']
    documentKey = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')

    #S3 client
    s3 = boto3.resource('s3')

    # Amazon Textract client
    textract = boto3.client('textract')

    # Get AWS Textract Response for Forms
    response = textract.analyze_document(
        Document={
            'S3Object': {
                'Bucket': bucketName,
                'Name': documentKey
            }
        },
        FeatureTypes = ["FORMS"])

    # Using custom trp module
    doc = Document(response)

    import csv 

    temp_csv_file = csv.writer(open("/tmp/csv_file.csv", "w+"))
    temp_csv_file.writerow(["Key", "Value"])

    for page in doc.pages:
        for field in page.form.fields:
            # This will write it as your <key>, <value>
            temp_csv_file.writerow([field.key, field.value])

    bucketName.upload_file('/tmp/csv_file.csv', 'textractData.csv')

模式:

exports.create = (req, res) => {
    let url = new Url(
        {
            fullUrl: req.body.fullUrl
        }
    );

    url.save(function (err, url) {
        if (err) {
            res.status(400).send(
                {
                    error: err,
                    requestBody: req.body
                }
            );
            return;
        }

        res.status(200).send(url);
    });
}

当前分支:https://github.com/FrisoDenijs/url-shortener/tree/dev

0 个答案:

没有答案