gpt4 book ai didi

node.js - 类型错误 : Cannot read property 'save' of null

转载 作者:行者123 更新时间:2023-11-30 12:44:17 25 4
gpt4 key购买 nike

我正在尝试将一些数据放入我编写的小型 Node 服务器中。

服务器端代码如下:

router.route('/returnLockID').put(function(req, res){
mongoOp.findOne({
name: req.body.name
}, function(err, user) { if(err) {
response = {"error" : true,"message" : "Error fetching data"};
} else {
// we got data from Mongo.
// change it accordingly.
if(req.body.LockID !== undefined) {
// case where ID needs to be updated.
user.LockID = req.body.LockID;
}
// save the data
user.save(function(err){
if(err) {
response = {"error" : true,"message" : "Error updating data"};
} else {
response = {"error" : false,"message" : "Data is updated for "+req.body.name};
}
res.json(response);
})
}
});
})

我收到回复:

{
"error": false,
"message": "Data is updated for nv942"
}

但是,数据并未更新。谁能看出我的储蓄出了什么问题吗?

当我使用 postman PUT 时,一切都很顺利,我可以保存,但是,当我尝试从 iOS 上 PUT 时,我得到:

TypeError: Cannot read property 'save' of null
at /Users/NikhilVedi/Documents/FYP/Server/lockserver/routes/users.js:92:13
at Query.<anonymous> (/Users/NikhilVedi/Documents/FYP/Server/lockserver/node_modules/mongoose/lib/model.js:3407:16)
at /Users/NikhilVedi/Documents/FYP/Server/lockserver/node_modules/kareem/index.js:259:21
at /Users/NikhilVedi/Documents/FYP/Server/lockserver/node_modules/kareem/index.js:127:16
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)

快速代码是:

   @IBAction func setup(_ sender: Any) {

if (UserDefaults.standard.value(forKey: "userIP") == nil)
{
//make this a message box and stop the program crashing by assigning user defaults a value
UserDefaults.standard.set("localhost", forKey: "userIP")

print("Local host programatically set");
}

let u = UserDefaults.standard.value(forKey: "userIP")!
let name = UserDefaults.standard.value(forKey: "email")!
var request = URLRequest(url: URL(string: "http://\(u):3000/users/returnLockID")!)
request.httpMethod = "PUT"
let postString = "LockID=\(LockID.text!)name=\(name)"
print(postString)
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}

if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}

let responseString = String(data: data, encoding: .utf8)
// print("responseString = \(responseString)")

if let data = responseString?.data(using: String.Encoding.utf8) {
let resString = JSON(data: data)

if resString["success"].stringValue == "true"
{
print("works");

}
else if resString["success"].stringValue == "false"
{
print("failed")
print(resString["message"].stringValue)
//dismiss window and set bool to true
UserDefaults.standard.set(true, forKey: "LockIDPresent")
self.dismiss(animated: true, completion: nil);
}

}

}
task.resume()
}

提前致谢!

最佳答案

我看到一个错误,但不太可能解决您的问题,那就是您应该在第一个 if 语句子句中执行 res.json(response);

另一件事需要注意的是,无论是否提供了 req.body.LockID 值,您都会调用 save ,因此在未提供的情况下用户被保存,不做任何修改。您可能想要打印出该值或 req.body.LockID,因为这可能是用户电子邮件未更新的原因。

关于node.js - 类型错误 : Cannot read property 'save' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41838829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com