无效使用“undefined”类型作为Firestore参数

时间:2018-06-14 11:02:16

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

执行代码时出错。下面用于在云Firestore中创建集合的代码,在此之前我没有在云Firestore中创建任何集合。它是空的。在console.log中,我得到了正确的json对象。

 createEntry(subjectId: string, questionnaire: string, callback: (err: any, response: any) => void) :void {
    let testEntity: TestEntity = new TestEntity(subjectId, questionnaire);
    console.log("testEntity.toJSON();----------->",testEntity.toJSON());
    var docRef = this.dbManager.collection('testQuestion').doc(subjectId);
var setAlan = docRef.set(testEntity.toJSON());
}

这里的TestEntity是json值,如下所示。所有字段都有值。

public toJSON(): testEntry {
    let returnJSON = {
        "entry_id": this.entry_id,
        "subject_id": this.subject_id,
        "entry_date": this.entry_date,
        "questionnaire": this.questionnaire,
        "entry_start_timestamp": this.entry_start_timestamp,
        "entry_end_timestamp": this.entry_end_timestamp,
        "entry_complete": this.entry_complete,
        "responses": this.responses,
        "last_answered_question" : this.last_answered_question,
        "entry_status" : this.entry_status
    }

    return returnJSON;
}

dbManager是this.adminInstance:

export class DatabaseManager{
    private static adminInstance:any;

    static getAdminInstance(): any {
    if(!this.adminInstance)
        admin.initializeApp(functions.config().firebase);
    this.adminInstance   =   admin;
    return this.adminInstance.firestore();
    }
}

我收到以下错误。怎么解决?

Error: Invalid use of type "undefined" as a Firestore argument.

   at Object.exports.customObjectError.val [as customObjectError] (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/validate.js:164:14)
    at Function.encodeValue (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/document.js:813:20)
    at Function.encodeFields (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/document.js:683:36)
    at Function.fromObject (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/document.js:223:55)
    at WriteBatch.set (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/write-batch.js:301:39)
    at DocumentReference.set (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/reference.js:420:8)
    at testEntryService.createEntry (/user_code/services/test-entry-service.js:19:30)
    at /user_code/services/intentService.js:22:36
    at Function.<anonymous> (/user_code/node_modules/actions-on-google/dist/service/dialogflow/dialogflow.js:146:23)
    at next (native)

由于我是Firebase的新用户,因此无法识别此错误。这个问题的根本原因是什么?

0 个答案:

没有答案