代码之家  ›  专栏  ›  技术社区  ›  Yosi Pramajaya

检测到“Timestamp”类型的对象与预期实例不匹配

  •  1
  • Yosi Pramajaya  · 技术社区  · 7 年前

    我想知道为什么Timestamp对象没有像我期望的那样工作?

    它在测试环境中工作(我使用Mocha),但在部署时抛出错误。

    索引.ts

    import { Timestamp, QuerySnapshot } from "@google-cloud/firestore";
    ....
    
    async someFunction() {
       let col = firestore.collection("mycollection");
       let now = Timestamp.now();
       let twentyMinsAgo = Timestamp.fromMillis(now.toMillis() - (1200 * 1000));
    
       return col
          .where('LastEdited', '>=', twentyMinsAgo) //This throws error
          .get()
    }
    

    堆栈跟踪

    Argument "value" is not a valid QueryValue. 
    Detected an object of type "Timestamp" that doesn't match the expected instance. 
    Please ensure that the Firestore types you are using are from the same NPM package.
          at Validator.(anonymous function).err [as isQueryValue] (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/validate.js:99:27)
          at CollectionReference.where (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/reference.js:940:25)
    

    "dependencies": {
       ....
       "@google-cloud/firestore": "^0.16.0",
       "firebase-admin": "~6.0.0",
       "firebase-functions": "^2.0.5"
    }
    
    1 回复  |  直到 7 年前
        1
  •  11
  •   Yosi Pramajaya    7 年前

    现在我明白它为什么会出错了。因为我单独导入Firestore对象,而我应该只使用Firebase Admin SDK中的Firestore对象。

    1. 从中删除“@google cloud/firestore”依赖项 包.json

    索引.ts

    async someFunction() {
       let col = firestore.collection("mycollection");
       let now = admin.firestore.Timestamp.now();
       let twentyMinsAgo = admin.firestore.Timestamp.fromMillis(now.toMillis() - (1200 * 1000));
    
       col.where('LastEdited', '>=', twentyMinsAgo) //Now ok
          .get()
    }
    
        2
  •  0
  •   Chukwuemeka Maduekwe    5 年前

    使用firebase admin时,避免直接导入和使用任何客户端包

    所以

     import * as admin  from "firebase-admin";
     import firebase from "firebase/app";
    
     admin.firestore().collection("name").set({
           date: firebase.firestore.Timestamp.now()
    })
    

    改用这个

     import * as admin  from "firebase-admin";
    
     admin.firestore().collection("name").set({
           date: admin.firestore.Timestamp.now()
    })