Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

Your function is missing { after (data)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda