woo
woo

Reputation: 11

Node.JS MongoDB SyntaxError: Invalid or unexpected token

I don't understand what is wrong. Node v.16.4.2, NPM v7.18.1

const mongoose = require("mongoose");
// const dotenv = require('dotenv')
require('dotenv').config({path:'variables.env'});

mongoose.connect(process.env.MONGODB_URL, { useNewUrlParser: true}, err => {
    if (err){
        console.log(err);
    } else {
        console.log("Connected to database successfully");

    }
})

/* mongoose.js */
const db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error'));
db.once('open', ()=>{
  console.log('DB connected');
});

const express = require('express');
const app = express();
app.get('/', (req, res) => {
  Cat.find({}, (err, healings) => {
    if(err) return res.json(err);
    res.json(healings);
  });
});
app.listen(3000, () => {
  console.log('Connect 3000');
});

const userSchema = mongoose.Schema({
    emotion: Number,
    image: String,
    placeName: String,
    grade: Number,
    id: mongoose.Schema.Types.ObjectId,
});
  ​
const Healing = mongoose.model("user", userSchema); // 스키마 등록
var test = new Healing({ emotion: 1111, image: 'test', placeName:'where', grade:5 });
test.save();

//  const Cat = mongoose.model('Kitty', CatSchema);

the error is

/Users/Downloads/test2/mongoose.js:41

SyntaxError: Invalid or unexpected token at Object.compileFunction (node:vm:352:18) at wrapSafe (node:internal/modules/cjs/loader:1025:15) at Module._compile (node:internal/modules/cjs/loader:1059:27) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10) at Module.load (node:internal/modules/cjs/loader:975:32) at Function.Module._load (node:internal/modules/cjs/loader:816:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12) at node:internal/main/run_main_module:17:47

Upvotes: 1

Views: 7962

Answers (2)

Joe
Joe

Reputation: 28316

The error calls out line 41, which appears to be the blank line immediately prior to

const Healing = ...

When I copy/paste that line into Vim, it shows <200b>, which suggests that line contains a zero-width space (properly encoded in UTF-8).

Upvotes: 1

James Young
James Young

Reputation: 326

I think it's an encoding issue.

Please see, for example: How to fix `SyntaxError: Invalid or unexpected token` when trying to run Node.js app

Try saving your source code file with UTF-8 encoding.

Upvotes: 1

Related Questions