Node.js “类型”的无效使用;“未定义”;作为Firestore的论点
我在执行代码时出错。下面的代码用于在CloudFireStore中创建集合,在此之前,我没有在CloudFireStore中创建任何集合。它是空的。在console.log中,我得到了正确的json对象Node.js “类型”的无效使用;“未定义”;作为Firestore的论点,node.js,firebase,google-cloud-firestore,google-cloud-functions,Node.js,Firebase,Google Cloud Firestore,Google Cloud Functions,我在执行代码时出错。下面的代码用于在CloudFireStore中创建集合,在此之前,我没有在CloudFireStore中创建任何集合。它是空的。在console.log中,我得到了正确的json对象 createEntry(subjectId: string, questionnaire: string, callback: (err: any, response: any) => void) :void { let testEntity: TestEntity = new
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值,如下所示。所有字段都有值
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());
}
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:
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());
}
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();
}
}
我正在犯错误。如何解决
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());
}
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)
错误:将类型“undefined”用作Firestore参数无效。
在Object.exports.customObjectError.val[作为customObjectError](/user_code/node_modules/firebase admin/node_modules/@google cloud/firestore/src/validate.js:164:14)
在Function.encodeValue(/user\u code/node\u modules/firebase admin/node\u modules/@google cloud/firestore/src/document.js:813:20)
在Function.encodeFields(/user\u code/node\u modules/firebase admin/node\u modules/@google cloud/firestore/src/document.js:683:36)
位于Function.fromObject(/user\u code/node\u modules/firebase admin/node\u modules/@google cloud/firestore/src/document.js:223:55)
在WriteBatch.set(/user\u code/node\u modules/firebase admin/node\u modules/@google cloud/firestore/src/write batch.js:301:39)
在DocumentReference.set(/user\u code/node\u modules/firebase admin/node\u modules/@google cloud/firestore/src/reference.js:420:8)
在testEntryService.createEntry(/user\u code/services/test entry service.js:19:30)
at/user_code/services/intentService.js:22:36
在功能上。(/user\u code/node\u modules/actions on google/dist/service/dialogflow/dialogflow.js:146:23)
在下一个(本地)
由于我是Firebase的新手,因此无法识别此错误。问题的根本原因是什么?可能重复的请查看以下QO问题a和答案:您是否可以编辑该问题以准确显示您试图在调用set()时放入文档的值?让setAlan=docRef.set({'entry_id':'7cf93279-de0f-497d-a789-0ae12804c19c','subject_id':subject','entry_date':'2018-06-15','investment':1','entry_start_timestamp':'2018-06-15T11:38:57.030Z','entry_end_timestamp':'2018-06-15T11:38:57.030Z','entry_complete':false,“最后一个问题已回答”:0,“条目状态”:“活动”});
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());
}