Currently, I am utilizing Node.js and Express on Heroku with the MongoDB addon. The database connection is functioning correctly as data can be successfully stored, but there seems to be an issue with pushing certain types of data.
Below is the database connection snippet:
mongodb.MongoClient.connect(mongoURI, function (err, database) {
if (err) {
console.log(err);
process.exit(1);
}
// Save database object from the callback for reuse.
db = database;
console.log("Database connection ready");
// Initialize the app.
var server = app.listen(process.env.PORT || dbport, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
});
I have no issues when pushing my Twitter API response into the database using the following code:
db.collection(TWEETS_COLLECTION).insert(data);
(where 'data' is a JSON variable)
However, attempting to push another JSON variable within the same method results in an error. Here's the code snippet:
var jsonHash = '{"hashtag":"","popularity":1}';
var objHash = JSON.parse(jsonHash);
objHash.hashtag = req.body.hashtag;
JSON.stringify(objHash);
collection(HASHTAG_COLLECTION).insert(jsonHash);
And here is the error that occurs:
TypeError: Cannot create property '_id' on string '{"hashtag":"myhash","popularity":1}' at Collection.insertMany... ...
Any insights into what might be causing this issue?