Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js AWS Lambda+;Firestore数据库:add()未在集合中插入文档数据_Node.js_Firebase_Google Cloud Firestore_Firebase Admin - Fatal编程技术网

Node.js AWS Lambda+;Firestore数据库:add()未在集合中插入文档数据

Node.js AWS Lambda+;Firestore数据库:add()未在集合中插入文档数据,node.js,firebase,google-cloud-firestore,firebase-admin,Node.js,Firebase,Google Cloud Firestore,Firebase Admin,我正在尝试在Nodejs的AWS Lambda上使用Firestore。以下是数据库连接的初始化部分: var admin = require('firebase-admin'); var serviceAccount = JSON.parse(process.env.cred); admin.initializeApp({ credential: admin.credential.cert(serviceAccount), }); var db = admin.firesto

我正在尝试在Nodejs的AWS Lambda上使用Firestore。以下是数据库连接的初始化部分:

var admin = require('firebase-admin');
var serviceAccount = JSON.parse(process.env.cred);
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),     
});
var db = admin.firestore();
但每当我尝试插入操作时,它都不会将数据插入“测试”集合中

插入代码:

async (events, context) => {
    try {            
              var firestoreResult = await db.collection('packagescan12').add({
                                    packageId: scan.packageId,                                  
                                    status: "unsynced",
                                    timestamp: new Date()
               });                            
               console.log('Added document with ID: ', firestoreResult.id);
    } catch(err) {                    
               console.log("Error in inserting: ", err);
    };
}
在日志中,它显示
“添加了ID为:undefined的文档”

编辑

正如Frank van Puffelen给出的答案所强调的那样,
databaseURL
不是初始化应用程序所必需的

在循环中插入两个文档时来自CloudWatch的日志:

Error in inserting:  Error: 4 DEADLINE_EXCEEDED: Deadline exceeded
    at Object.callErrorFromStatus (/opt/nodejs/node_modules/@grpc/grpc-js/build/src/call.js:31:26)
    at Object.onReceiveStatus (/opt/nodejs/node_modules/@grpc/grpc-js/build/src/client.js:176:52)
    at Object.onReceiveStatus (/opt/nodejs/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:342:141)
    at Object.onReceiveStatus (/opt/nodejs/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:305:181)
    at /opt/nodejs/node_modules/@grpc/grpc-js/build/src/call-stream.js:124:78
    at processTicksAndRejections (internal/process/task_queues.js:79:11)
Caused by: Error
    at WriteBatch.commit (/opt/nodejs/node_modules/@google-cloud/firestore/build/src/write-batch.js:426:23)
    at DocumentReference.create (/opt/nodejs/node_modules/@google-cloud/firestore/build/src/reference.js:285:14)
    at CollectionReference.add (/opt/nodejs/node_modules/@google-cloud/firestore/build/src/reference.js:2021:28)
    at /var/task/index.js:170:80
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async AsyncIterableX.[Symbol.asyncIterator] (/opt/nodejs/node_modules/ix/asynciterable/operators/map.js:18:28)
    at async Object.toArray (/opt/nodejs/node_modules/ix/asynciterable/toarray.js:18:22) {
  code: 4,
  details: 'Deadline exceeded',
  metadata: Metadata { internalRepr: Map {}, options: {} },
  note: 'Exception occurred in retry method that was not classified as transient'
}
然后我尝试打开数据库URL:“https://database_id_here.firebaseio.com,并在浏览器中显示“未找到”作为响应

这是预期的行为。Firebase有两个数据库:云Firestore和实时数据库。您的代码使用的是Firestore API,而URL是用于实时数据库的,这显然不是为该项目创建的

由于您只是试图在代码中使用Firestore,所以未创建实时数据库这一事实并不是问题。事实上,我建议从代码中删除该URL,因为它不是必需的,而且因为它会让您感到困惑,可能会让其他人感到困惑


要查看Firestore数据库,请转到htts://firebase.google.com/console,选择项目,然后在左侧导航中选择Firestore。

请共享插入代码。请注意,
databaseURL:“https://database_id_here.firebaseio.com“
是与实时数据库相对应的URL,而不是Firestore。@RenaudTarnec我已进行了编辑,并记录了有关数据库URL的信息。我也在编辑中添加了一些信息。你没有在Firestore控制台中看到新的文档吗?没有,正如我前面提到的,它说从你共享的代码中添加了id:Undefinedf,应该保存在数据库中。再次注意,Firebase中有两种不同的数据库服务。查看控制台时,注意不要混淆RTDB和Firestore。您应该共享整个代码,以便我们进一步帮助您。哦,我明白了,这是有意义的。我认为
databaseURL
可能是错误的,它导致数据库连接设置不正确,因此没有在集合中插入数据。因为,它不再是必需的,我编辑了这个问题。add()是否没有“等待”插入,只是忽略了该操作?我也发布了错误发生的日志。