createJson不是一个函数

时间:2017-08-18 09:25:29

标签: node.js

我正在尝试创建一个机器人功能,使其能够存储计划ID以及当前作业,多年的经验和用户的工作数量。但是出现了“createJson不是函数”。我已宣布是一个功能。并且列表并没有停在那里,但是日志会话因某个问题而停止,请为此我需要很多帮助。感谢...

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



exports.helloHttp = functions.https.onRequest((req, res) => {


    if (req.body.result && (req.body.result.parameters || req.body.result.action || req.body.result.contexts)) {
        var userQuery = req.body.result.resolvedQuery || {};
        var action = req.body.result.action || {};
        var parameters = req.body.result.parameters || {};
        var plan_id = req.body.result.parameters.plan_id || {};

        createJson();
        var data = JSON.stringify(cJson);
        fs.writeFileSync('Psedo JSON.json', data);


        var createJson = function() {
                userProfile();
                var time = moment().format();
                var cJson = {
                    timeStamp: time,
                    lang: 'en',
                    userInfo: {
                        planId: iPlanId,
                        currentWork: iCurJob,
                        workExp: iYearExp,
                        numberJob: iNumJob
                    }
                };
            }

        var userProfile = function() {

                if (action == "get_plan_id") {
                    inPlanId();
                    if (action == "update_user_info" && cur_job !== null && num_job !== null && yr_exp !== null) {
                        inCurJob();
                        iNumJob();
                        inYearExp();
                    }
                }
            }

        var inPlanId = function() {
            var iPlanId = req.body.result.parameters.plan_id;
            return [iPlanId];
        }
        var inCurJob = function() {
            var cur_job = req.body.result.parameters.cur-job || {};
            var iCurJob = req.body.result.parameters.cur-job;
            return [iCurJob];
        }
        var inYearExp = function() {
            var yr_exp = req.body.result.parameters.yr-exp || {};
            var iYearExp = req.body.result.parameters.yr-exp;
            return [iYearExp];
        }
        var inNumJob = function() {
             var num_job = req.body.result.parameters.number-job || {};
                var iNumJob = req.body.result.parameters.number-job;
                return [iNumJob];
            }

    }

    res.setHeader('Content-Type', 'application/json'); //Requires application/json MIME type
    res.send(JSON.stringify({
        "speech": response,
        "displayText": response

    }))
})

1 个答案:

答案 0 :(得分:0)

函数表达式没有像其他变量那样提升值。

如果你想让它用值提升你应该使用函数语句

 function createJson() {
            userProfile();
            var time = moment().format();
            var cJson = {
                timeStamp: time,
                lang: 'en',
                userInfo: {
                    planId: iPlanId,
                    currentWork: iCurJob,
                    workExp: iYearExp,
                    numberJob: iNumJob
                }
            };
        }

其他功能也一样。



console.log(typeof x, typeof y)

var x = function(){}
function y() {}