简体   繁体   中英

How to push string into a null array

I am facing some problem when I try to push an string into an null array. I have pasted my code snippet down below. Could anyone please help me by checking it and tell me where I have done wrong ?
Thank you very much. This is my git repository : https://github.com/zymethyang/NodeJs_HKUST could you help me check it. Express server in conFusionServer folder, database in MongoDB and conFusion-Angular4 is webview. Username is administrator, password :1234.

.post(cors.corsWithOptions, authenticate.verifyUser, (req, res, next) => {
    Favorites.findOne({ user: req.user._id })
        .then((favorite) => {
            var dishId = req.params.dishId;
            if (favorite != null) {
                if ((favorite.dishes.indexOf(dishId)) < 0) {
                    favorite.dishes.push(dishId);
                }
                favorite.save()
                    .then((favorite) => {
                        res.statusCode = 200;
                        res.setHeader('Content-Type', 'application/json');
                        res.json(favorite);
                    }, (err) => next(err));
            } else {
                favorite.dishes.push(dishId);
                favorite.save()
                .then((favorite) => {
                    res.statusCode = 200;
                    res.setHeader('Content-Type', 'application/json');
                    res.json(favorite);
                }, (err) => next(err));
            }
        }, (err) => next(err))
        .catch((err) => next(err));
})

Handle error with my comment below.

.post(cors.corsWithOptions, authenticate.verifyUser, (req, res, next) => {
    Favorites.findOne({ user: req.user._id })
        .then((favorite) => {
            var dishId = req.params.dishId;
            if (favorite != null) {

                // Please handle error with initial variable.
                if (!favorite.dishes) { favorite["dishes"] = []; }

                if ((favorite.dishes.indexOf(dishId)) < 0) {
                    favorite.dishes.push(dishId);
                }
                favorite.save()
                    .then((favorite) => {
                        res.statusCode = 200;
                        res.setHeader('Content-Type', 'application/json');
                        res.json(favorite);
                    }, (err) => next(err));
            } else {

                // Please handle error with initial variable.
                favorite["dishes"] = [];

                favorite.dishes.push(dishId);
                favorite.save()
                .then((favorite) => {
                    res.statusCode = 200;
                    res.setHeader('Content-Type', 'application/json');
                    res.json(favorite);
                }, (err) => next(err));
            }
        }, (err) => next(err))
        .catch((err) => next(err));
})

You can check if an element is an array by calling

if (favorite != null) {
    if (favorite.dishes != null && Array.isArray(favorite.dishes)) {
        favorite.dishes.push(value)
    } else {
        favorite.dishes = [value]
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM