Firebase函数onCreate不会写入Firestore,但https.onRequest将-相同的代码

时间:2019-05-11 22:36:26

标签: javascript firebase google-cloud-firestore google-cloud-functions

我有一个利用承诺的功能。

我已经通过https.onRequest()执行了代码,并将其写入数据库,但是,当我在document()。onCreate()上使用相同的代码时,它突然停止,并且在函数日志“无视已完成的函数的异常”,没有其他日志条目。

该代码在注释“ //服务器状态代码”之后停止执行。在控制台中,我看到来自服务器的选民响应JSON,后跟状态代码。我试图使用collection()。doc()。set()和.add()。 在日志的前面,我收到“函数返回的未定义,期望的Promise或值”错误,该错误未在https.onRequest()中得到。该代码与.onCreate()代码的例外相同,但从一开始就从数据库中提取了一些其他信息。

任何帮助将不胜感激。

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.testonupdate = functions.firestore.document("users/{userId}/voterRegistration/infoToSubmit").onCreate((snap, context) => {
console.info('New voter info has been added to check verification')
const voterValues = snap.data();
console.info('database values: ' + voterValues);
const voterInfo = {
    'firstName': voterValues.firstName,
    'lastName': voterValues.lastName,
    'dob': voterValues.dob,
    'street': voterValues.street,
    'city': voterValues.city,
    'zip': voterValues.zip
}
const userID = voterValues.userId
console.info('voter info values: ' + voterInfo + userID) 

var request = require('request');
var jar = request.jar();
var voteUrl = 'https://'
var utahApi = 'https://'
var xsrfOptions = {
    url: voteUrl,
    method: 'get',
    jar: jar
};
// initial promise
var options = new Promise((resolve, reject) => {
    request.get(xsrfOptions, () => {
        var fullCookie = jar.getCookies(voteUrl);
        resolve(fullCookie);

    })
})

options.then((value) => {
    //parse cookie returned from var options and dump into headers
    var cookieToString = value.toString()
    var xsrfCookie = cookieToString.slice(0, 47)
    var slicedCookie = cookieToString.slice(11, 47)
    console.info('slicedCookie :' + slicedCookie)
    //POST request data
    return {
        uri: utahApi,
        headers: {
            'Host': 'votesearch.utah.gov',
            'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0',
            'Accept': 'application/json, text/plain, */*',
            'Accept-Language': 'en-US,en;q=0.5',
            'Accept-Encoding': 'gzip, deflate, br',
            'Referer': voteUrl,
            'Content-Type': 'application/json;charset=utf-8',
            'X-XSRF-TOKEN': slicedCookie,
            //'Content-Length': '300', LEAVE THIS COMMENTED OUT OR SERVER WILL NOT RESPOND
            'Connection': 'keep-alive',
            'Cookie': xsrfCookie,
            'Pragma': 'no-cache',
            'Cache-Control': 'no-cache'
        },
        method: 'POST',
        json: true,
        //user info goes here
        body: {
            'firstName': voterValues.firstName,
            'lastName': voterValues.lastName,
            'dob': voterValues.dob,
            'street': voterValues.street,
            'city': voterValues.city,
            'zip': voterValues.zip
        }
    }
}).then((options) => {
    console.info('starting request...')
    // voter registration request
    request(options, (error, response, body) => {
        if (!error && response.statusCode === 200) {
            console.info(response.body.voter); // voter response
            console.info(response.statusCode); // server status code
            admin
                .firestore()
                .collection("users")
                .doc("userID")
                .set(response.body);


        } else {
            console.info('failed response code ' + response.statusCode);
            console.info('failed at retreiving voterInfo ' + error);

        }
    });
    // return console.info('did we finish?')
    //catch any error messages
}).catch((err) => {
    console.info('errored at end of function: ' + err);
})


})

1 个答案:

答案 0 :(得分:0)

request(options, (error, response, body) => {.....创建新的承诺似乎可以解决问题。