Ad
Can't Post To Firebase's Database
When I try to post data with postman to firebase I get this error: Error: Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value (found in field body).
I checked and body and userHandle comes back as undefined but I don't know why. With postman I use POST and in the body I send a json like this:
{
"body": "scream 2",
"userHandle": "user2"
}
as raw JSON(application/json)
const functions = require('firebase-functions');
const admin = require('firebase-admin');
let firebaseConfig = {
apiKey: '*******',
authDomain: '******',
databaseURL: '*****',
projectId: '*****',
storageBucket: '****',
messagingSenderId: '*****',
appId: '*******'
};
admin.initializeApp(firebaseConfig);
exports.createScream = functions.https.onRequest((req, res) => {
if (req.method !== 'POST') {
return res.status(400).json({ error: 'Method not allowed' });
}
const newScream = {
body: req.body.body,
userHandle: req.body.userHandle,
createdAt: admin.firestore.Timestamp.fromDate(new Date())
};
admin
.firestore()
.collection('screams')
.add(newScream)
.then(doc => {
res.json({ message: `document ${doc.id} created successfully` });
})
.catch(err => {
res.status(500).json({ error: 'something went wrong' });
console.error(err);
});
});
Ad
Answer
Okay it's a little weird but in postman the body was set to raw JSON (application/json)but I set it again to JSON(application/json) and now it's working.
Ad
source: stackoverflow.com
Related Questions
- → Maximum call stack exceeded when instantiating class inside of a module
- → Browserify api: how to pass advanced option to script
- → Node.js Passing object from server.js to external modules?
- → gulp-rename makes copies, but does not replace
- → requiring RX.js in node.js
- → Remove an ObjectId from an array of objectId
- → Can not connect to Redis
- → React: How to publish page on server using React-starter-kit
- → Express - better pattern for passing data between middleware functions
- → Can't get plotly + node.js to stream data coming through POST requests
- → IsGenerator implementation
- → Async/Await not waiting
- → (Socket.io on nodejs) Updating div with mysql data stops without showing error
Ad