我正在构建一个角度节点应用评级系统。在该系统中,我试图循环遍历作为POST请求发送到服务器的id数组,并使用for循环将这些id推送到用户需要对列进行评级。
第一个用户成功获取整个循环,但下一个用户在第二个索引处失败。经过调试,我发现后端正在接收所有正确的变量,我不明白为什么它不适用于用户2而是用于用户。
这是我的后端代码
addRatee: function addRatee(req,res) {
var decoded = jwt.decode(req.query.token);
return model.User.findById(req.params.raterId)
.then(function (user) {
for (let ratee of req.body.rateeIds) {
user.needToRate.push(ratee);
}
user.update({
needToRate: user.needToRate
}).then(function (updatedUser) {
return res.status(200).json({
title: "Successfully added users to need to rate",
obj: updatedUser
});
}).catch(function () {
return res.status(400).json({
message: "Error updating user",
error: err.toString()
});
});
}).catch(function (e) {
return res.status(400).json({
message: "Error finding user",
error: e.toString()
});
});
}
这是我的组件服务代码
addRatee(rateeIds: number[], raterId: number) {
const headers = new Headers({'Content-Type': 'application/json'});
const token = localStorage.getItem('token')
? '?token=' + localStorage.getItem('token')
: '';
const body = {
rateeIds: rateeIds
};
return this.http.patch(this.devUrl + '/new-ratee/' + raterId + token, body, {headers: headers});
}
这是我的component.ts代码
let dRateeIds = [this.trip.rider1Id, this.trip.rider2Id, this.trip.rider3Id];
let r1RateeIds = [this.trip.driverId, this.trip.rider2Id, this.trip.rider3Id];
let r2RateeIds = [this.trip.rider1Id, this.trip.driverId, this.trip.rider3Id];
let r3RateeIds = [this.trip.rider1Id, this.trip.rider2Id, this.trip.driverId];
this.tripService.addRatee(dRateeIds, this.trip.driverId)
.subscribe(updatedUser => {
console.log(updatedUser);
this.tripService.addRatee(r1RateeIds, this.trip.rider1Id)
.subscribe(updatedUser2 => {
console.log(updatedUser2);
this.tripService.addRatee(r2RateeIds, this.trip.rider2Id)
.subscribe(updatedUser3 => {
console.log(updatedUser3);
this.tripService.addRatee(r3RateeIds, this.trip.rider3Id)
.subscribe(updatedUser4 => {
console.log(updatedUser4);
}, resp => console.log(resp));
}, resp => console.log(resp));
}, resp => console.log(resp));
}, resp => console.log(resp));
我对编程很新,所以我的代码有点混乱,我只是在寻找发生此错误的原因。
这是给出的完整错误堆栈。
{"message":"Error finding user","error":"TypeError: Cannot read property 'push' of null\n at /Users/jonathancorrin/Desktop/workspace/atlas-web/server/controllers/trips.js:1380:24\n at tryCatcher (/Users/jonathancorrin/Desktop/workspace/atlas-web/node_modules/bluebird/js/release/util.js:16:23)\n at Promise._settlePromiseFromHandler (/Users/jonathancorrin/Desktop/workspace/atlas-web/node_modules/bluebird/js/release/promise.js:512:31)\n at Promise._settlePromise (/Users/jonathancorrin/Desktop/workspace/atlas-web/node_modules/bluebird/js/release/promise.js:569:18)\n at Promise._settlePromise0 (/Users/jonathancorrin/Desktop/workspace/atlas-web/node_modules/bluebird/js/release/promise.js:614:10)\n at Promise._settlePromises (/Users/jonathancorrin/Desktop/workspace/atlas-web/node_modules/bluebird/js/release/promise.js:693:18)\n at Async._drainQueue (/Users/jonathancorrin/Desktop/workspace/atlas-web/node_modules/bluebird/js/release/async.js:133:16)\n at Async._drainQueues (/Users/jonathancorrin/Desktop/workspace/atlas-web/node_modules/bluebird/js/release/async.js:143:10)\n at Immediate.Async.drainQueues (/Users/jonathancorrin/Desktop/workspace/atlas-web/node_modules/bluebird/js/release/async.js:17:14)\n at runCallback (timers.js:781:20)\n at tryOnImmediate (timers.js:743:5)\n at processImmediate [as _immediateCallback] (timers.js:714:5)"}
这是我的pgadmin表在几次尝试失败后的样子。空值为用户2-4
******更新 以下是在配置之前记录needsToRate列和值类型
时记录的值用户需要评估====== null USER TYPE NTR ========== object
答案 0 :(得分:1)
这个问题是我试图将值推送到空字段。我用这段代码解决了这个问题
for (let ratee of req.body.rateeIds) {
if (user.needToRate === null) {
user.needToRate = [];
user.needToRate.push(ratee)
} else {
user.needToRate.push(ratee)
}
}