【问题标题】:AngularJS Date string vs object errorAngularJS日期字符串与对象错误
【发布时间】:2017-11-29 06:52:30
【问题描述】:

我正在尝试检索一组用户信息(通过搜索名字)并返回名字和预订日期(来自 mongoDB,猫鼬模式使用日期:日期)。名字是 type=text,预订日期是 type=date。它返回未定义的日期,错误消息为 Error: [ngModel:datefmt] Expected `2017-11-06T16:00:00.000Z to be a date。我读了其他问题/答案,这是因为 type=date 需要一个对象,但 JSON 返回一个字符串。

我对 Angular JS 真的很陌生,我已经尝试了建议的方法,但没有结果。我不知道缺少什么。根据我的代码欣赏任何特定的指针

HTML

<form name="updateBookingsForm" ng-repeat="booking in ctrl.bookings" novalidate>
    <input name="firstname" type="text" ng-model="booking.firstname" class="form-control" required>
    <input name="date" type="date" ng-model="booking.date" class="form-control" required>
</form>

控制器

self.searchAllBookings = function () {
    paAppAPI.searchAllBookings(self.term).then(function (result) {
        console.log(result); //returns array and date undefined
        self.bookings = result;
        }

    }).catch(function (err) {
        console.log(err);
        if (err.status == 404) {
            self.message = "No bookings found";
            self.showMessage = true;
        }
    });
}

服务

self.searchAllBookings = function (term) {
    var defer = $q.defer();

    $http.get("/api/booking?keyword=" + term).then(function (result) {
        console.log(result); //returns array but date undefined
        if (result.status == 200) {
            defer.resolve(result.data);
        } 
        else {
            defer.resolve(null);
        }

    }).catch(function (err) {
        console.log(err);
        defer.reject(err);
    });

    return defer.promise;
}     

服务器

app.get("/api/booking?", function (req, res) {

    console.log("Search booking > " + req.query.keyword);
    var keyword = req.query.keyword;

    Booking.find({ 
        "firstname" : new RegExp('^'+keyword+'$', "i")
    }, (err, result) => {
        if (err) {
            console.log(err);
        }
        res.status(200).json(result);

        console.log(result); 
        // { _id: 5a1a4e1238dfaa65e5fa59a2,
        //    firstname: 'Emily',
        //    date: 2017-11-20T16:00:00.000Z,
        //    __v: 0 },

        console.log(result[0].date); //2017-11-06T16:00:00.000Z

    });
});     

【问题讨论】:

    标签: javascript angularjs json mongodb date


    【解决方案1】:

    我假设self.bookings 是一个数组。如果是这样,需要在将字符串转换为日期之前对其进行循环。

    如果日期未定义,则分配当前日期。否则将字符串转换为日期。

    .then(function (result) {
        console.log(result); //returns array but date is undefined
        self.bookings = result;
        for(var i=0; i< self.bookings.length; i++){
           self.bookings[i].date = new Date(self.bookings[i].date) : Date.now())
        }
    
    
    })
    

    【讨论】:

    • 添加此行后,我在控制台记录 self.booking.date,我确实得到了日期,但它没有被传递给服务(日期未定义)
    • 您想将日期传递给服务还是在 html 中查看?
    • 谢谢!有效!我的代码出错了,你的 FOR 语句是正确的!!!
    • @miintea 只是一个说明,您还可以从您的服务函数searchAllBookings 返回 $http.get 调用的结果,而不是创建您自己的延迟($get 函数返回一个 HttpPromise,这主要是就像您通过创建自己的延迟对象获得的常规承诺一样)创建自己的延迟对象对于缓存服务中的内容仍然很有用,但以防万一您没有注意到。
    • @shaunhusain 同意了。因为http返回一个promise,所以不需要创建一个新的(使用defer对象)
    猜你喜欢
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多