Amit Sinha
Amit Sinha

Reputation: 1183

how to save geopoint in firestore using query

I tried to save geopoint in Firestore collection but it throw error. Here is my code.

var post = req.body;    
let data = {
    name: !cmnFn.empty(post.name) ? post.name : '',
    user_location: (new admin.firestore.GeoPoint(28.52038,77.28073)),
    created_at: d.getTime()
}
// Saving data
db.collection('users').doc(post.uid).set(data);

Error which i am receiving

Error: Cannot encode type ([object Object]) to a Firestore Value

Upvotes: 1

Views: 2397

Answers (1)

Amit Sinha
Amit Sinha

Reputation: 1183

Now I am able to save geo point. See the complete code to achieve this.

const admin = require("firebase-admin");
const serviceAccount = require(process.env.FBASE_AUTH_FILE);
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://<PROJECT_ID>.firebaseio.com"
});
const Firestore = admin.firestore;
const db = Firestore();
let d = new Date();
let data = {                
    name: 'Amit',
    location: new admin.firestore.GeoPoint(28.52038,77.28073),
    created_at: d.getTime()
}
setDoc = db.collection('users').doc().set(data);
if (setDoc) {
    console.log('user added');
}

Upvotes: 1

Related Questions