错误:函数返回的值未定义,预期的承诺或价值

时间:2019-07-08 14:45:08

标签: node.js firebase express google-cloud-firestore google-cloud-functions

我用firebase函数设置了一个pubsub函数,以便在Firestore中经常执行一些操作。为此,我需要向第三方API发出请求以获取更新的数据,然后将这些数据插入Firestore中的正确集合和文档中。

const request_promise = require('request-promise')

exports.scheduledFunction = functions.pubsub.schedule('every 2 minutes').onRun((context) => {
    console.log('This will be called every 2 minutes')
    var username = ''
    var password = ''
    var options = {
        url: 'path.to.api.com',
        auth: {
            user: username,
            password: password
        },
        json: true
    }

    request_promise(options)
        .then(function (product) {
            console.log(product.product_id)
            db.collection('products').doc(product.product_id).set(product)
                .then(() => {
                    console.log('Document successfully written')
                })
                .catch(error => {
                    console.log('Error writing product to firestore', error)
                })
        })
        .catch(function (err) {
            console.log('Failed to get product', error)
        })
  });

在上面的代码中,如果我注释掉将数据添加到Firestore的调用,则正确的product_id将打印到控制台,因此我知道请求正在运行,但仍留在其中,我得到了“函数返回了未定义的期望的承诺或价值”。

2 个答案:

答案 0 :(得分:1)

执行时不返回任何内容。 console.log不被视为return

request_promise(options)
        .then(function (product) {
            console.log(product.product_id)
            // add an implicit return here
            return db.collection('products').doc(product.product_id).set(product)
                .then(() => {
                    console.log('Document successfully written')
                    // need's to return something here, using a boolean for simplicity
                    return true;
                })
                .catch(error => {
                    console.log('Error writing product to firestore', error)
                    // throw will exit the function call
                    throw Error('Error writing product to firestore', error);
                })
        })
        .catch(function (err) {
            console.log('Failed to get product', error);
            // throw will exit the function call
            throw Error('Failed to get product', error);
        })

答案 1 :(得分:1)

通过正确的promise链接,这看起来更干净

      rp(options)
        .then((product) =>
        {
            console.log(product.product_id)
            // add an implicit return here
            return db.collection('products').doc(product.product_id).set(product)
        })
        .then(() =>
        {
            console.log('Document successfully written')
            // need's to return something here, using a boolean for simplicity
            return true;
        })
        .catch(function (err) 
        {
        console.log('Failed to get product', error);
        // throw will exit the function call
        throw Error('Failed to get product', error);
        });

也不建议从catch块中抛出错误,catch块用于捕获错误并处理它们,而不是抛出错误。您的代码中需要改进的地方很少,但这不是这个问题的一部分

干杯, 快乐编码