0

i can get the json file content and display the object content but i cannot display json content when the json file has more than 1 object inside.

here is how it looks when i have one object only: enter image description here

and that how it looks when i have more than 1 object on the database.json enter image description here

and if you got a solution here is my client js code

  
    
    var jsonn = '/database.json';

    async function userinfo() {
        
        const response = await fetch(jsonn);
        const data = await response.json();
        var { username, password, _id } = data;
        document.getElementById('user').textContent = username;
        JSON.stringify(jsonn);
        console.log(data);
        console.table (data);
        console.log (username);
        console.log (_id);
    }
    
    userinfo();
    

and here is my server.js

var express = require("express");
var app = require("express")();
var http = require("http").Server(app);
var io = require("socket.io")(http);
var port = process.env.PORT || 3000;
var router = require("express");
var fs = require("fs");
var DatasStore = require("nedb");
//test
var batteryLevel = require('battery-level');
var externalip = require('externalip');
//database test 
var Database = new DatasStore('database.json');
Database.loadDatabase();
//Database.insert({ "name" : "Rahim", "status" : "Online"});
//Database.insert({ "name" : "Rasko", "status" : "Offline"});
app.post('/new', function (req, res) {
    new user({
        "name": "username"
        , "password": "password"
        , "class": "classes"
        , "gender": "gender"
    });
    var usr = (user);
    Database.insert(usr);
});
//Accessebale Files//
app.use(express.static("public/chat.html/"));
//console.log(Database);
app.use('/icon', express.static(__dirname + '/public/icon.png'));
app.use('/home', express.static(__dirname + '/public/home.html'));
app.use('/chat', express.static(__dirname + '/public/chat.html'));
app.use('/login', express.static(__dirname + '/public/login.html'));
app.use('/hometest', express.static(__dirname + '/public/hometest1.html'));
app.use('/index.js', express.static(__dirname + '/public/index.js'));
app.use('/database.json', express.static(__dirname + '/public/database.json'));
app.use(express.static(__dirname + "/login.html"));
console.log(__dirname);
app.get("/", function (req, res) {
    res.sendFile(__dirname + "/public/login.html");
    console.log("users ip ↓");
    console.log(req.ip);
    Database.insert("{" + "Users-ip:" + req.ip);
});
io.on("connection", function (socket) {
    socket.on("chat message", function (msg) {
        io.emit("chat message", msg);
    });
});
http.listen(port, function () {
    console.log("|---------------------------------------------------|");
    console.log("|----------------------SERVER-----------------------|");
    console.log("|---------------------------------------------------|");
    console.log("");
    console.log("The Server is Live on PORT : " + port);
    console.log("           ")
    console.log("");
});
io.on("connection", socket => {
    console.log("a user connected");
    console.log("------------------------------");
    socket.on("disconnect", () => {
        console.log("user disconnected");
        console.log("----------------------------");
    });
});
io.on("connection", socket => {
    socket.on("chat message", msg => {
        console.log("Sent: " + msg);
        Database.insert((msg));
    });
});
//testing code
//BatteryLevelLogger//
batteryLevel().then(level => {
    console.log(level);
    Database.insert(level);
});
///ServerStatus Logger
const https = require('http');
var options = {
    host: '192.168.1.102'
    , port: 3000
    , path: '__dirname'
};
https.get(options, function (res) {
    console.log("status: " + res.statusCode);
}).on('error', function (e) {
    console.log("error: " + e.message);
});
Database.insert(batteryLevel);
//26august test//
//app.post('/', (request, response) => {
//  console.log("requestse");
//    app.post();
//});
///LOGIN TEST///
//var dataa = fs.readFileSync("database.json");
//var info = JSON.parse(dataa);
//Database.loadDatabase();
//fs.writeFile("database.json" , info);
//console.log(info);///
////Login///
const Datastore = require('nedb');
app.listen(2000, () => console.log('listening at 3000'));
app.use(express.static('/public'));
app.use(express.json({
    limit: '1mb'
}));
const database = new Datastore('public/public/database.json');
database.loadDatabase();

app.post('/public', (request, response) => {
    const data = request.body;
    const timestamp = Date.now();
    data.timestamp = timestamp;
    database.insert(data);
    response.json(data);
    console.log(data);
    var logdate = new Date;
    console.log(logdate);
});

i don't really want to edit server.js because every think is working in the server and i guess it's the client code that should read more than 1 object on the json file "database.json" again if you have an answer please consider sharing it with me.

here is the json file "with 2 object":

{"username":"eaz","password":"","email":"eza","timestamp":1598667968928,"_id":"BSA8MsS2dU299vcT"}
{"username":"user","password":"random","email":"emailandom","timestamp":1598667968928,"_id":"BSA8MsS2dU299vcT"}
11
  • 2
    May you share the JSON which is giving that error when parsed? Commented Aug 29, 2020 at 20:31
  • 4
    The content of database.json has to be an array of objects: [ { ... }, { ... }, ... ] Commented Aug 29, 2020 at 20:36
  • i tried add the [{content}] but it didn't work Commented Aug 29, 2020 at 20:37
  • 1
    The current database.json file does not contain valid JSON. Commented Aug 29, 2020 at 20:37
  • what makes the diffrence Commented Aug 29, 2020 at 20:38

1 Answer 1

1

A JSON file can only have 1 main object. You can make it work if you write it like this:

[
    {"username":"eaz","password":"","email":"eza","timestamp":1598667968928,"_id":"BSA8MsS2dU299vcT"},
    {"username":"user","password":"random","email":"emailandom","timestamp":1598667968928,"_id":"BSA8MsS2dU299vcT"}
]

Also, all elements in a JSON file must be separated by a comma.

Sign up to request clarification or add additional context in comments.

1 Comment

but how can i display the second object content ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.