0

I was following instructions to build a RESTful API with Express and MySQL(*1) But when I change

app.listen(port); //excutable, GET returns welcome message

into

orm.initialize(WConfig.config,function(err,models){
...

in the part 2 of(*1), which is adding MySQL information into server.js, I gets the following on Node.JS command prompt:

TypeError: Cannot read property 'bear' of undefined

Because this is the first attempt in building RESTful API, I'm not sure what to do to fix it. Help please! Any idea is appreciated. full code of server.js: // server.js

var util = require('util');

var express    = require('express');     
var app        = express();                 
var bodyParser = require('body-parser');
var Waterline    = require('waterline');

var Bear         = require('./app/models/bear');
var WConfig = require('./app/config/waterline');

var orm = new Waterline();
orm.loadCollection(Bear);

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var port = process.env.PORT || 8080;        

var router = express.Router();             

router.get('/', function(req, res) {
    res.json({ message: 'hello! welcome to our api!' });   
});

app.use('/api', router);
// express deprecated res.json(obj,status): use res.status(status).json(obj) instead 
router.route('/bears')
    .post(function(req,res) {
        app.models.bear.create(req.body,function(err,model) {
            if(err) return res.status(500).json({ err,err });
            res.json(model);   //res.json(model) , guess: res.status(200).json(model);
            console.log(util.inspect(model));
        });
    });

//gets error if I change it to following 
//
orm.initialize(WConfig.config,function(err,models){
    if(err) throw err;
    app.models = models.collections;
    //app.set('models',models.collections);
    app.connections = models.connections;

    app.listen(port);

    console.log('Magic happens on port ' + port);
});

reference: 1.Create Restful API with Express and waterline (in Chinese) https://segmentfault.com/a/1190000004996659#articleHeader2

2.Build a RESTful API Using Node and Express 4 https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4

陳冠宇
  • 1
  • 3
  • Can you post your code instead of the reference articles? – juliano.net Oct 04 '16 at 18:44
  • Sorry, the code has been added now. – 陳冠宇 Oct 05 '16 at 05:22
  • What is the line number that is causing the error? – juliano.net Oct 05 '16 at 12:16
  • I found a error with comment, then checked then again and run. server.js can run for now, until I send POST http://localhost:8080/api/bears the error message is: express deprecated res.json(obj,status): use rers.status(status).json(obj) instead at server.js:31:32 – 陳冠宇 Oct 05 '16 at 13:05
  • Yep, `res.json` has been deprecated on Express 4.7.0. Did you try with `res.status(status).json(obj)` as suggested? – juliano.net Oct 05 '16 at 13:13
  • I tried to change them, the error is throw err; Rethrow non-MySQL errors. at server.js: 32, res.json(model); has no status, and I leave unchanged. – 陳冠宇 Oct 05 '16 at 13:30
  • Try using `res.status(200).json(model);`. I don't have a node.js environment set to test your code, so this is just a guess. – juliano.net Oct 05 '16 at 13:35
  • Thank you, just it shows the same error when I send POST. The following is the full error, hope it helps identifying http://i.imgur.com/D8hDMSC.png – 陳冠宇 Oct 05 '16 at 14:00
  • From the screenshot the error actually seems to be related to the model you are trying to convert to json. See the message `TypeError: Converting circular structure to JSON`. – juliano.net Oct 05 '16 at 14:27
  • So it's either sails-mysql,body-parser or waterline caused the trouble? I'm not sure what's next, try different models perhaps ? P.S: I've wrote waterline.js and bear.js similar to the reference, maybe they have something to do with it? – 陳冠宇 Oct 05 '16 at 14:56
  • Actually I think it's none of them. I think the problem is your model. Check this example https://makandracards.com/makandra/28847-dealing-with-typeerror-converting-circular-structure-to-json-on-javascript – juliano.net Oct 05 '16 at 15:03
  • Sorry, I'm not quite understand the article, but I found that they said console.log(util.inspect(obj)) can resolve the problem at http://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json ,so I put it in line 33, but still no luck :( – 陳冠宇 Oct 05 '16 at 15:58
  • have updated the code in the article – 陳冠宇 Oct 05 '16 at 16:36

0 Answers0