{
"error": {
"statusCode": 500,
"name": "TypeError",
"message": "Cannot read property 'connector' of undefined",
"stack": "TypeError: Cannot read property 'connector' of undefined\n at Function.Projects.getProjectData (F:\\SanWork\\AngularWorkSpace\\jobMngt\\common\\models\\projects.js:7:44)\n at SharedMethod.invoke (F:\\SanWork\\AngularWorkSpace\\jobMngt\\node_modules\\strong-remoting\\lib\\shared-method.js:263:25)\n at HttpContext.invoke (F:\\SanWork\\AngularWorkSpace\\jobMngt\\node_modules\\strong-remoting\\lib\\http-context.js:389:12)\n at phaseInvoke (F:\\SanWork\\AngularWorkSpace\\jobMngt\\node_modules\\strong-remoting\\lib\\remote-objects.js:654:9)\n at runHandler (F:\\SanWork\\AngularWorkSpace\\jobMngt\\node_modules\\loopback-phase\\lib\\phase.js:135:5)\n at iterate (F:\\SanWork\\AngularWorkSpace\\jobMngt\\node_modules\\loopback-phase\\node_modules\\async\\lib\\async.js:146:13)\n at Object.async.eachSeries (F:\\SanWork\\AngularWorkSpace\\jobMngt\\node_modules\\loopback-phase\\node_modules\\async\\lib\\async.js:162:9)\n at runHandlers (F:\\SanWork\\AngularWorkSpace\\jobMngt\\node_modules\\loopback-phase\\lib\\phase.js:144:13)\n at iterate (F:\\SanWork\\AngularWorkSpace\\jobMngt\\node_modules\\loopback-phase\\node_modules\\async\\lib\\async.js:146:13)\n at F:\\SanWork\\AngularWorkSpace\\jobMngt\\node_modules\\loopback-phase\\node_modules\\async\\lib\\async.js:157:25\n at F:\\SanWork\\AngularWorkSpace\\jobMngt\\node_modules\\loopback-phase\\node_modules\\async\\lib\\async.js:154:25\n at execStack (F:\\SanWork\\AngularWorkSpace\\jobMngt\\node_modules\\strong-remoting\\lib\\remote-objects.js:493:7)\n at RemoteObjects.execHooks (F:\\SanWork\\AngularWorkSpace\\jobMngt\\node_modules\\strong-remoting\\lib\\remote-objects.js:497:10)\n at phaseBeforeInvoke (F:\\SanWork\\AngularWorkSpace\\jobMngt\\node_modules\\strong-remoting\\lib\\remote-objects.js:650:10)\n at runHandler (F:\\SanWork\\AngularWorkSpace\\jobMngt\\node_modules\\loopback-phase\\lib\\phase.js:135:5)\n at iterate (F:\\SanWork\\AngularWorkSpace\\jobMngt\\node_modules\\loopback-phase\\node_modules\\async\\lib\\async.js:146:13)"
}
}
使用此代码时出现此错误,我想知道导致错误的原因和任何可能的修复。
型号代码:
'use strict';
module.exports = function(Projects) {
Projects.getProjectData = function(cb) {
var sql = 'select * from projects';
// call sql query from postreSQL
Projects.app.datasources.postgres.connector.query(sql, function(err, result) {
if (err) {
console.log('Error retrieving projects result ...');
console.log(err);
return cb(err);
}
cb(err, result[0].jsondata);
});
}
Projects.remoteMethod(
'getProjectData', {
returns: [{
type: 'string',
required: true,
root: true
}],
http: {
verb: 'get'
}
}
);
};
我已经安装了loopback-connector-postgresql&生成模型,但是当我尝试编写自定义API时出错。
答案 0 :(得分:1)
module.exports
声明之前
var app = require('../../server/server.js');
这是假设您位于公共文件夹中的模型目录中。
然后使用Projects.app.datasource
而不是app.datasource
使用[HttpPost]
public ActionResult changePassword(PersonalPageModel personalPage)
{
var passwordDetauls = dbModel.USERS.Where(model =>
model.vPassword == personalPage.usersModel.NewPassword
).FirstOrDefault();
if (passwordDetauls != null)
{
personalPage.usersModel.ChangePasswordErrorMessage = "Новый пароль совпадает со старым!";
return View("~/Views/home/PersonalPage.cshtml", personalPage);
}
else
{
var userDetails = dbModel.USERS.Where(model =>
model.vPassword == personalPage.usersModel.vPassword).FirstOrDefault();
if (userDetails == null)
{
personalPage.usersModel.LoginErrorMessage = "Неверный пароль!";
return View("~/Views/home/PersonalPage.cshtml", personalPage);
}
else
{
int iIdUsers = Convert.ToInt32(Session["iIdUsers"].ToString());
//Change password here!
var query = from ord in dbModel.USERS
where ord.iIdUsers == iIdUsers
select ord;
foreach (var ord in query)
{
ord.vPassword = personalPage.usersModel.NewPassword;
}
try
{
dbModel.SaveChanges();
}
catch(Exception exc)
{
personalPage.usersModel.ChangePasswordErrorMessage = exc.Message;
}
ViewBag.SuccessMessage = "Пароль был успешно изменен!";
//Session["iIdUsers"] = userDetails.iIdUsers;
//Session["vNickName"] = userDetails.vNickName;
//return RedirectToAction("PersonalPage", "Home");
return View("~/Views/home/PersonalPage.cshtml", personalPage);
}
}
}