Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
Connection to Mongodb through Node.js

Hii Everyone

In my code, There is an API that works fine. And I'm trying to connect to MongoDB from Node for the sake of inserting the data from the API.

As you can see, I get this error - "message": "Uncaught SyntaxError: Unexpected identifier", it looks like there is a problem with MongoClient.

I looked at the answers on this site about those topics and no one solves my problem. Thanks for any help!

let http = require('http');
let weatherKey = process.env.weatherKey;
// console.log(weatherKey);

function getData(cb) {

    http.get(`http://api.openweathermap.org/data/2.5/weather?q=israel&appid=${weatherKey}`, res => {
        let data = ""
        res.on('data', string => {
            data += string
        })
        res.on('end', () => {
            let obj = JSON.parse(data)
            cb(obj);
        })

    }).on("error", error => {
        cb(error);
    })
}

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://127.0.0.1:27017/";

function connectToMongo(data)
MongoClient.connect(url, function (err, db) {
    if (err) throw err;
    var dbo = db.db("weather-db");
    dbo.collection("node").insertOne(data, err => {
        if (err) throw err;
        console.log("documents inserted");
        db.close();
    });
});

getData(connectToMongo);

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You were missing {} in your code.

let http = require('http');
let weatherKey = process.env.weatherKey;

function getData(cb) {

    http.get(`http://api.openweathermap.org/data/2.5/weather?q=israel&appid=${weatherKey}`, res => {
        let data = ""
        res.on('data', string => {
            data += string
        })
        res.on('end', () => {
            let obj = JSON.parse(data)
            cb(obj);
        })

    }).on("error", error => {
        cb(error);
    })
}

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://127.0.0.1:27017/";

function connectToMongo(data){

    MongoClient.connect(url, function (err, db) {
        if (err) throw err;
        var dbo = db.db("weather-db");
        dbo.collection("node").insertOne(data, err => {
            if (err) throw err;
            console.log("documents inserted");
            db.close();
        });
    });
}

getData(connectToMongo);
about 4 years ago · Juan Pablo Isaza Report

0

Your function is missing { after (data)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!